Running Nodejs applications in production forever vs supervisord vs pm2

I've been running a few production Nodejs websites for a while now, including this one. I was using forever but struggled with random crashes of apps and not recovering. I then switched to supervisord with more success but still had issues stopping/starting and general non auto restarting apps.

supervisord (Link)

Pros

  • Mature software
  • Works on many different environments

Cons

  • Not Nodejs specific
  • Hard to setup
  • I found it was unreliable recovering my Nodejs apps
  • limited options
forever (Link)

Pros

  • Mature software (7.5k Github stars)
  • Works on many different environments
  • Easy to setup

Cons

  • I found it was unreliable recovering my Nodejs apps
  • Had issues with apps being auto started after a system reboot
  • Limited options (at the time of writing)
pm2: my pick (Link)

Pros

  • Mature software (12.5k Github stars)
  • Can be used with an enterprise (paid) add-on service called Keymetrics
  • Works on many different environments
  • Super easy to setup
  • Many different options to scale apps in cluster mode etc
  • Command pm2 list give an easy to read table of all apps

Cons

  • A little difficult setting up as a non root user
  • None that I've found so far. Rock solid.

Some pm2 basics

Firstly you need to install it globally using the "-g" flag via NPM:

npm install pm2 -g

You can then start apps using the start commmand. I found passing the "-n" flag followed by a nice name of the app helps to identify the app later:

pm2 start app.js -n "appname"

You can get a nice list output of apps using the list command:

pm2 list

You can also run your app in cluster mode which spools off multiple processes. If you are wanting to run 4 process you app can be started like:

pm2 start app.js -n appname -i 4

After a clustered app is started you can scale up or back using:

pm2 scale appname 2

You can then manage your apps by specifying the command followed by the "id" from the "pm2 list" command:

Restart

pm2 restart 0

Stop

pm2 stop 0

Delete

pm2 delete 0

I found the logging fantastic too. You can easily tail the logs to get any errors:

pm2 logs

Remember to run the following command to save the processes for when the server is rebooted:

pm2 save

Get out there and give pm2 a try.