ralyodio / node-startup

Startup script for Linux-based systems for running node app when rebooting using an /etc/init.d script.
MIT License
781 stars 169 forks source link

I'm getting the wrong pid in AWS linux #25

Open dennisa opened 8 years ago

dennisa commented 8 years ago

Hi,

I was wondering if you could help me out but I'm using your script but the generate pid always seems to get 2 number below the real process id of the running node app.

i.e. the reported pid is 20195 but when I tried to do a ps -aux | grep node, i could see the pid is actually 20197.

Do you know what might be going wrong

jeffklassen commented 8 years ago

I am experiencing the same problem in AWS linux.

ralyodio commented 8 years ago

echo \$! > $PID_FILE" its just echoing the pid created from the command to a pid file. I'm not sure why it would be wrong. Might want to ask in #bash on freenode.

jeffklassen commented 8 years ago

If you replace get_pid() with the following:

get_pid() {
    echo "$(ps aux | grep node | awk 'NR==1{print $2}')"
}

Each command works perfectly.

ralyodio commented 8 years ago

That is not safe. There could be other node processes other than the one in the pid file that you don't want to kill. If you can give me a simple command that produces the wrong pid using (<cmd> ) &; echo $! i'll ask the bash guys in irc

jeffklassen commented 8 years ago

ah, that's of course correct. Thanks for checking into that. So, the following produces the correct pid:

# watch 'ls -lrat' & echo $!
[3] 7694
root      7694  0.0  0.3 116128  4044 pts/2    T    08:26   0:00 watch ls -lrat

I'm not sure whats going on here.

ralyodio commented 8 years ago

so it created the pid as 7694 and that's what you're seeing in the ps -ef output.

also i'm wondering if you have any forks/clustering or suexec commands in your app.

jeffklassen commented 8 years ago

well ps aux | grep ls, but yes.

ralyodio commented 8 years ago

I don't use this script anymore but its become popular. can you tell me what your settings are for the environment variables at the top of the script? I can try to reproduce.

Also, test your startup with a simple do { console.log('test') } while (true); in your app.js. Just to rule out your app isn't causing the issue.

kerber commented 8 years ago

I'm actually having this problem on CentOS 6 and Amazon Linux. it appears that the PID being stored is not that of node, but the parent bash process that is spun off when the script starts node. Killing this bash process seems to leave the node app running.

In order to reliably kill off the node child, pkill can be used, which will kill children to a given process ID. I've sent a pull request for the change.

dbellavista commented 8 years ago

Hi! It's a problem of operator priority order. If you do A && B & echo $!, you're getting the PID of the process A && B. The init init script does: cd $APP_DIR && node $APP & echo $! > $PID

So the PID belongs to the bash process performing the cd and node. To solve, just put the command between parentheses:

cd $APP_DIR && (node $APP & echo $! > $PID)

FossPrime commented 6 years ago

This was fixed in #50 #51 @dennisa