Service manager, PM2

How to manage production services like a charm.

First step is to install it as a global npm module:

1
# npm i pm2 -g

It works like Forever but PM2 is better I think. May be there users whose say the opposite, but I prefer PM2.

Getting started

Assuming your main file is app.js, usually you start your project with node app.js or npm start which is calling to node app.js. With PM2 you should start your project as follow:

1
# pm2 start app.js

Now that you have your instance running you can check it with:

1
# pm2 list

Keep online after restart

After you have started your project you should execute the following commands:

1
2
# pm2 startup
# pm2 save

The command pm2 startup generates the script and configures the autoboot after a restart. The command pm2 save saves the list of services currently running.

Basic examples

Start an app using all CPUs available + set a name:

1
# pm2 start app.js -i 0 --name "api"

Restart the previous app launched, by name:

1
# pm2 restart api

Stop the app:

1
# pm2 stop api

Restart the app that is stopped:

1
# pm2 restart api

Remove the app from the process list:

1
# pm2 delete api

Kill daemon pm2:

1
# pm2 kill

Update pm2:

1
# pm2 update

References