Pickels Pickels - 1 year ago 120
Node.js Question

Forever.js starting and restarting multiple scripts

My web app has 3 main node.js components: website, feeds and jobs.

To start these I am using forever:


var forever = require('forever');

function start(name){

forever.start( ['coffee', name + '.coffee'], { /* log options */ } )



What I first noticed is that if I run script it wont run it as a daemon. ( Which is most likely normal )

node forever.js

So what I did next was run the forever.js script with forever. I am not sure if this is correct, there is also a forever.startDaemon so not sure which one I should use.

forever start forever.js

This works but the problem is that I would like to restart all the processes when a new version of my app is published. I am using git's post-receive hook to run the forever.js the first time but if I do this on each post-recieve it will just spawn 3 processes each time.

So I guess I need a way to restart 3 processes if they are already running. I thought to do this with forever.list but the documentation only say:

forever.list (format, callback)

Returns a list of metadata objects about each process that is being run using
forever. This method is synchronous and will return the list of metadata as such.
Only processes which have invoked forever.startServer() will be available from

First of all I am not sure what format means and second it expects a callback but then it says its synchronous. Which is a little confusing and I am not sure how to use list.

In the end all I want to do is start/restart 3 node.js processes on git's post-receive hook.

Answer Source

I think the best way to do this is:

forever start website.js
forever start feeds.js
forever start jobs.js

and then in your git post-receive hook:

forever restart website.js
forever restart feeds.js
forever restart jobs.js

Wrapping these node processes inside a single process is not a good idea. I now personally use Supervisord with monit instead of forever (supervisord is more stable & powerful than forever IMHO).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download