My case is this:
I am running a node.js server at domain.com:54321
The command I use to start the server is:
forever start -l forever.log -a -o out.log -e err.log index.js
However, there are some cases where our code gets into a high demanding function, causing the script to work very slow or unresponsive. We are trying to optimize it.
In that case, I stop the server, and start a new one if needed, lets say domain.com:67890
forever stop 0
But if I want to start again the recently stopped node.js server domain.com:54321 (or restart it instead of stopping it) with this, I was expecting the processes to stop and run as fresh and fast again.
The thing that if I start it again, or restart it, it continues to go high on resources. I found out that I need to leave it for a couple of hours to start it again.
My question is, are there any other commands that will make sure that every instance, resource of that server is stopped, so I can start using it again immediately?
Thank you
You can stop a Forever daemon using the id of forever running using four different ways:
index
id
uid
pid
I can see one of the answer to covering the stopping a forever running daemon using pid, I can add for other methods.
List running Forever instances:
$ forever list
info: Forever processes running
|data: | index | uid | command | script |forever pid|id | logfile |uptime |
|------|-------|-----|------------------|-------------|-----------|-----|------------------------|--------------|
|data: | [0] |f4Kt |/usr/bin/nodejs | src/index.js|2131 | 2146|/root/.forever/f4Kt.log | 0:0:0:11.485 |
$ forever stop 0 index
$ forever stop 2146 id
$ forever stop --uid f4Kt uid
$ forever stop --pidFile 2131 pid
To make sure to stop the process you can use the pid option:
forever start -l forever.log -a -o out.log -e err.log --pidFile ~/app_pid index.js
forever stop --pidFile ~/app_pid
Run forever list then forever stop **id**
Here is a sample output
$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 9Xzw ng serve --host 0.0.0.0 --port 300 1111 2312 /home/ec2-user/.forever/9Xzw.log 1:3:20:50.412
data: [1] wOj1 npm run-script app-start-dev 29500 24978 /home/ec2-user/.forever/wOj1.log 0:0:5:3.433
Check your process id and kill
forever stop 0 OR forever stop 1 OR forever stop 2
Here 0, 1, 2 index of process, data:[0], data:[1]
May be kill all node processes forcefully if that is what you want.
follow this answer to kill all node process : https://stackoverflow.com/a/14790921/5055850
check this, if your port are 8000 , it will stop forever
sudo kill -9 `sudo lsof -t -i:8000`
I had the same problem. For me this worked:
forever stopall.
Related
user 1550 1 0 Jun19 ? 00:00:00 node /usr/bin/nodemon src/server.js
user 11051 1550 0 Jun19 ? 00:00:38 /usr/bin/node src/server.js
Can someone tell how this process running in the background? The user is not logged in.
it seems you are running your nodejs Application using nodemon
nodemon is used to do auto reload when an application crashed. because nodejs is single thread application, if you did not catch the error safely, it will crash the application. as nodejs is used for server-side application, its needed to use a tool which will do auto restart when its crashed.
you can kill the process by running the below command.
(mine ubuntu):
$ kill -9 <proceessId> //here it will nodemon process it
I had a node process running with pm2. It showed up in the result of pm2 list. Now I see the process isn't running anymore, and pm2 list is empty.
Are there any pm2 logs which show me when and why it stopped running my node process?
Well first of all you have the pm2 logs command that will show you logs, but pm2 is running by the user that ran the pm2 start commant. Meanning that if you were in sudo mode and ran pm2 start you will not see the application list from a regular user.
If this is not the issue, you should know that if you restart your machine, the list deletes. if you want to keep the list after restart, you need to run pm2 save after you finish starting your applications/json configuration file.
I'm using forever.js to make sure my Node.js / Express app runs without a break. However, sometimes it crashes and forever doesn't restart it.
I'm using
forever start app.js
to start the app
and it starts fine, works, and then at some point crashes and when I do
forever list
it doesn't list anything, so it simply doesn't restart...
I also tried running it with a log file, using
forever start -l foreverlog.txt app.js
and the log file is fine, but it doesn't show any info about the end of the process - e.g. the crash or error report, which I usually have if I run the app.js from my console directly.
Do you know how I could make forever restart the app or at least get the errors into the log?
Thank you!
Forever splits the log
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
Even if you didn't specify the -e file it should be somewhere. Users/{UserName}/.forever on windows.
You should check the ERRFILE. Maybe that will bring some light to why the process failed.
You need to use spinSleepTime:
forever --minUptime 1000 --spinSleepTime 1000 -w -l *.log -e *.log --port 1234 app.js
--minUptime not set. Defaulting to: 1000ms
--spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
Try killing the process manually by searching for the process with
ps axl | grep node
then
kill 24597
replace 24597 with your process number. Then reboot forever again.
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
I am deploying two node.js apps on the aws, the two apps are in the paths shown as
/home/ubuntu/nodes/app1/app.js
/home/ubuntu/nodes/app2/app.js
respectively
to run the node.js apps in the background, I used forever to start two apps, so like
$ sudo forever start /home/ubuntu/nodes/app1/app.js
$ sudo forever start /home/ubuntu/nodes/app2/app.js
so forever works well by running the two node.js apps in the background process.
However, when I tried to stop one process with forever command like this.
$ sudo forever stop /home/ubuntu/nodes/app1/app.js
unexpectedly, both node.js process are closed with info like this
info: Forever stopped process:
data: uid command script forever pid logfile uptime
[0] r2pZ /usr/bin/nodejs app.js 24852 24854 /root/.forever/r2pZ.log 0:0:1:14.775
[1] 9f2h /usr/bin/nodejs app.js 24870 24872 /root/.forever/9f2h.log 0:0:0:58.733
I assume it is because two node.js process has the same name - app.js, how to avoid this by close only one process
You can use an uid (see here):
$ sudo forever --uid "app1" start app.js
$ sudo forever --uid "app2" start app.js
And to stop:
$ sudo forever stop app1
Update
The --uid option is deprecated.
Now you can use the --pidFile option. Example:
forever start --pidFile /some/path/app1.pid app.js
forever start --pidFile /some/path/app2.pid app.js
And to stop:
forever stop --pidFile /some/path/app1.pid
You can kill only one process using the index of the process shown in the forever list command. For example, if you type forever stop 1, only the process with the index 1 will be killed
I start node.js server has 3000 port by nohup for background running, but now i want to stop the node.js server with 3000 and start again with the same port. How can I stop the process?
i can use this command for get the process to kill;
ps -a","ps -ef |grep nohup
but it return;
-bash: kill: (11929) - No such process
thanks
Finally i got an solution for kill the node server which is run under nohup process
by the help of
start server # background: https://stackoverflow.com/a/4018223/2616818
pid: https://stackoverflow.com/a/14152730
Kill: https://stackoverflow.com/a/8007409/2616818
1.Get the node process id by use
ps -ef | grep "node"
//9500 pts/0 00:00:00 .node.bin
2.Kill the process by
kill 9500
//you can use the PID to kill if not use kill -9 pid
for advanced use forever