3

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.

darksky
  • 20,411
  • 61
  • 165
  • 254
  • I would guess that your node application is not actually dying. Is it possible that something is keeping it alive? I use forever myself and can verify that it works as intended assuming the node process stops. – tier1 Jun 24 '14 at 18:57
  • I am putting a syntax error at the top of my file and I am seeing the Node syntax error in the file, and it says Forever detected script exited with code 8. Meaning, the Node application exited. Is your forever also returning non-zero, or how are you detecting it? – darksky Jun 24 '14 at 18:59
  • Notice that your process id has changed after running 'forever list' after the crash. I think that your script is starting up correctly, however, forever will stop trying to restart if your app crashes within the first few seconds of execution. This may be what you are running into here. – tier1 Jun 24 '14 at 19:01
  • It changed because these are two different runs. One is a proper one and the other has syntax errors. – darksky Jun 24 '14 at 20:00
  • @tier1 Does your forever return non-zero if node app fails? Do you actually sleep? Because the node app crashes on startup, which is after forever returns. So I don't think your forever does actually return non-zero.. How are you handling that? – darksky Jun 24 '14 at 20:03

1 Answers1

0

You might also take a look at the NON cli version of forever called :

forever-monitor 
https://github.com/nodejitsu/forever-monitor

which provides all the necessary events to subscribe to like :

error [err]: Raised when an error occurs
start [process, data]: Raised when the target script is first started.
stop [process]: Raised when the target script is stopped by the user
restart [forever]: Raised each time the target script is restarted
exit [forever]: Raised when the target script actually exits (permenantly).
stdout [data]: Raised when data is received from the child process' stdout
stderr [data]: Raised when data is received from the child process' stderr
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104