Last Updated: February 25, 2016
· sluzky

Node.js (forever-monitor) get exit code

Recently I was trying to use forever to keep a server running in Node.js.
My server has a graceful exit logic, when sent the right command, and I needed forever-monitor to stop monitoring the server when that happened.

I chose to use a specific exit code from the server process to communicate to the forever process that a graceful exit was reached, but there was no documented event in forever-monitor that lets you get that exit code.

Digging through the forever code I found an event that will do that, named exit:code.

So your code can look like this:

var child = new (forever.Monitor)('your-server-file.js', {
    max: 3,
    silent: true,
    options: []

child.on('exit:code', function (code) {
   if (code == 505) // graceful exit code

Have a fresh tip? Share with Coderwall community!

Post a tip