PM2 to control your Node.js applications

PM2 logoPM2 is an advanced Node.js process manager that make easy to manage all your Node.js applications such as “start|stop|reload|delete”.

 

Some of pm2’s features are:

  • Behavior configuration
  • Source map support
  • PaaS compatible
  • Watch & Reload
  • Log management
  • Monitoring
  • Module System
  • Cluster mode
  • Hot reload
  • Development workflow
  • Startup scripts
  • Deployment workflow
  • Keymetrics monitor
  • API
  • …and much more

For installation we will use npm such as:

npm install pm2 -g

This will install pm2 globally!

To set pm2 for starting a new app is enough to:

pm2 start index.js --name "My app" -i max 

# => index.js is app's index script 
# => --name "My app" is the name of the app that will appear on pm2 monitor 
# => -i max sets the CPUs number to use:
        max - use all CPUs 
        -1 - use all CPUs 
        <number> - use only <number> of CPUs

To start a Node.js app in production environment you must set it in ~/.profile file such as:

echo "export NODE_ENV=production" >> ~/.profile
source ~/.profile

Use pm2 monit to monitor all your apps such as:

pm2-monit

image from http://pm2.keymetrics.io/

Some of other commands are:

  • pm2 stop [id/name] – stops [id/name] app
  • pm2 restart [id/name] – restarts [id/name] app
  • pm2 delete [id/name] – delete [id/name] app from pm2 control (do not removes app’s code from your server!)
  • pm2 list – lists all applications configured with pm2
  • pm2 logs – show logs
  • sudo pm2 startup <centos|ubuntu|amazon> – make a startup script and install it to run pm2 at system’s startup

For more informations about pm2 you can go to GitHub’s page