PM2 - Node.js process management

XKCD

So you have spent a good time building your node application in the dev environment. Transferring the application from dev to prod environment is a different ball game altogether. When the app runs in a cloud server hosted in a remote location, the ease of debugging, logging and mannaging the app which comes while developing locally, is lost.

PM2 or Process Manager 2 is an incredibly versatile production-level process manager for NodeJs apps. It provides the following benefits out of the box:

  • Restart after Crashes: Restart your apps after it crashes due to any reason.
  • Alive forever: You can make your apps auto-start at system boot. Thus allowing for High Availability (HA) configurations.
  • Real time Monitoring: Provides real good developer experience to manage and monitor the running processes.
  • Load balancing and clustering: PM2 has a built in Load Balancer and even allows you to run your apps in cluster mode.

Getting started

First thing we need to do is to install PM2 globally on the machine.

npm i -g pm2

Next, to start a process, pm2 start <script> --name <name for running process>

pm2 start app.js --name app                # equivalent to node app.js
pm2 start npm -- run start:prod            # equivalent to npm run start:prod

[PM2] Starting /usr/bin/npm in fork_mode (1 instance)
[PM2] Done.

┌──────────┬────┬──────┬──────┬───────┬────────┬─────────┬─────┐
│ App name │ id │ mode │ pid  │ status│ restart│ uptime  │ cpu │
├──────────┼────┼──────┼──────┼───────┼────────┼─────────┼─────┤
│ app      │ 0  │ fork │ 807  │ online│ 0      │ 0m      │ 0%  │
└──────────┴────┴──────┴──────┴───────┴────────┴─────────┴─────┘
 Use `pm2 show <id|name>` to get more details about an app

With this, your app is now running under PM2, and if it crashes, PM2 will restart it.

To enable persistance through restarts i.e to ensure it starts again when rebooting the system, you can execute pm2 save to save the current configuration.

pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in C:\Users\moose\.pm2\dump.pm2

Now, whenever the system restarts, PM2 will start whatever processes we had running when we last ran pm2 save.

Managing processes

To list the status of all application managed by PM2, run pm2 [list|ls|status]

pm2 ls

┌──────────┬────┬─────────┬──────┬─────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
├──────────┼────┼─────────┼──────┼─────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
│ mm       │ 0  │ N/A     │ fork │ 807 │ online │ 0       │ 49m    │ 0%   │ 1.2 MB    │ pi   │ disabled │
│ vision   │ 1  │ N/A     │ fork │ 809 │ online │ 0       │ 49m    │ 0.9% │ 20.8 MB   │ pi   │ disabled │
└──────────┴────┴─────────┴──────┴─────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

The following is a list of some common process (single or all) management commands you should take note of:

pm2 restart app_name          #restart app with name app_name
pm2 restart all               #restart all apps

pm2 stop app_name             #stop app with name app_name
pm2 stop all                  #stop all apps

pm2 delete app_name           #kill and delete app with name app_name
pm2 delete 1                  #kill and delete app with ID 1
pm2 delete all                #kill and remove all apps

pm2 save                      #save current process list on reboot
pm2 startup                   #enable PM2 to start at system boot
pm2 unstartup                 #disable PM2 from starting at system boot

For logging

pm2 logs                      #view logs for all processes
pm2 logs 1                    #view logs for app 1

Monitoring

PM2 has a provision of a realtime dashboard that fits directly into the terminal:

pm2 monit

PM2 Monit

Further Resources