Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node server is running after forever stopall

I have a node application running as a daemon on the server with forever. After one of the updates, I tried to stop it (to restart later). But to my surprise forever stopall has done nothing No forever processes running was returned. Forever list has returned the same. Both commands I tried with sudo as well.

The problem is that I can clearly see that node is still running (my app is working just fine). Any idea what was wrong?

P.S. I needed to roll update as fast as possible so I just rebooted the server. But I am still curious about this situation.

P.S.S after typing ps aux | grep app.js

ubuntu    1320  0.0  2.2 663040 23232 ?        Ssl  Sep12   0:00 /usr/bin/nodejs /usr/lib/node_modules/forever/bin/monitor node_app/app.js
ubuntu    1322  0.0  6.9 992564 70792 ?        Sl   Sep12   0:31 /usr/bin/nodejs /var/www/node_app/app.js
root      9739  0.0  0.0  10468   936 pts/0    S+   11:09   0:00 grep --color=auto app.js

Why is this happening? I am running node app.js on amazon aws.

like image 575
Salvador Dali Avatar asked Sep 12 '14 10:09

Salvador Dali


1 Answers

SOLUTION 1 - Use of --uid

I´ve managed how to mark your apps in forever using a UID, and then not using forever stopall. This is a cleaner way, and will make forever to kill all the processes depending on the script.

You just need to add the --uid "scriptID" parameter, and then all the processes depending on it, will be controlled together.

To start a new daemon: forever start --uid "demo" --sourceDir /home/procdir -l /home/log/logfile -o /home/log/outputfile -a -d -v taskName

To Stop a daemon: forever stop -uid "demo"

-bash-4.1$ forever list
info:    Forever processes running
data:        uid       command       script           forever pid   id logfile                                     uptime          
data:    [1] Test /usr/bin/node grunt serve:test 18217   18224    /home/admin/logs/test/forever.log 59:20:21:10.512 
data:    [2] Dev  /usr/bin/node grunt serve:dev 18347   18354    /home/admin/logs/dev/forever.log  59:20:19:56.87  
data:    [3] Prod /usr/bin/node grunt serve:prod 20411   20418    /home/admin/logs/prod/forever.log           59:18:58:28.697 

Anyway, you can also run (hands way) this command to kill the processes: First, kill all forever tasks (this will prevent forever to run the task again when killing it):

 forever list | grep your_app | `awk '/\[0\]/{print "forever stop "$8}'` 

After that, when forever is killed, then now it´s time to kill your node_app

ps -efa | grep  node | grep your_app | `awk '{ print "kill "$2}'` 

I strongly recommend you to not using kill. forever --uid will surely be the best solution.

Hope this solution helps you!

SOLUTION 2 - forever --uid was deprecated

As forever --uid was deprecated, I´ve tried to find the way of managing multiple apps by name (without using the --uid deprecated method). It seems using config files is the solution. As read in forever docs uid and id parameters are still in use in config files. After some tests, I´ve managed that id is the right parameter:

The example:

-bash-4.1$ pwd
/jome/myuser/app
-bash-4.1$ forever list
info:    No forever processes running
-bash-4.1$ forever start ./forever/development.json 
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: index.js

-bash-4.1$ forever stop app4U
info:    Forever stopped process:
    uid  command             script   forever pid   id    logfile                          uptime      
[0] BaoO /usr/local/bin/node index.js 41196   41197 app4U /Users/me/.forever/BaoO.log 0:0:0:5.995 

forever/development.json

{
    // Comments are supported
    "id": "app4U",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/jome/myuser/app"
}

index.js

a=0;
while (a<10) {
    a+=1
    a-=1
}
like image 57
Alejandro Teixeira Muñoz Avatar answered Oct 02 '22 19:10

Alejandro Teixeira Muñoz