I am writing an init script for my Node.js application that is running using forever. Forever is a daemon itself, that runs the node process under it.
My issue is that when the Node.js application crashes, the init script does not catch that as forever always returns 0.
I am starting forever as follows: $FOREVER start --pidFile $PIDFILE -al $LOGFILE -ao $OUTFILE -ae $ERRFILE $SERVER
When the Node.js startups properly and I do a list, I get:
# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] UP0C /usr/bin/node /some/path/here/server.js 27322 27324 /root/.forever/UP0C.log 0:0:6:3.493
When it crashes, I see:
# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] QFel /usr/bin/node /some/path/here/server.js 27578 27580 /root/.forever/QFel.log 0:0:1:6.207
As you can see, there is almost no difference. When I tail that log file shown in the output, I see the error that node throws. I'll see error: Forever detected script exited with code: 8
at the end.
Checking the pid of the failed Node process, I see:
# ps aux | grep 27580
root 27671 0.0 0.0 103240 840 pts/0 S+ 18:37 0:00 grep 27580
The process is in a sleeping state. Why doesn't forever catch that and what is that process that is sleeping? Why doesn't it just exit?
In other words, using forever, how can I detect that the process that forever tried to run failed? Why doesn't forever have an easy way to manage that? Isn't forever, in essence, designed to manage starting and stopping Node applications? Checking whether the process actually started should be one of the cornerstone features.