Nodejs PM2 Task runner overview

As we know, node.js applications are single threaded. It brings some challenges of its own kind. What if your program throws any exception which are not handled or there are some unrecoverable errors or bugs? It will simply result in application process to be killed which is not acceptable when you want your applications to be highly available.

In Node.js world, this was a common problem and thats where task runners such as Forever, PM2 solved these problems. In this blog, we will cover PM2.

PM2 is an advanced task runner and provides following functionalities out of the box

  • Manage application start/stop lifecycle. When your application stops due to some reason, PM2 reruns it again to bring it up again.
  • Scale application up or down with a simple command.
  • PM2 also acts as a load balancer.

Directory structure


Once PM2 is started, it will automatically create below directories:

$HOME/.pm2 - contain all PM2 related files
$HOME/.pm2/logs - contain all applications logs
$HOME/.pm2/pids - contain all applications pids
$HOME/.pm2/pm2.log - PM2 logs
$HOME/.pm2/pm2.pid  - PM2 pid
$HOME/.pm2/rpc.sock - Socket file for remote commands
$HOME/.pm2/pub.sock - Socket file for publishable events
$HOME/.pm2/conf.js PM2 Configuration

Common Commands


Start application

# Fork mode
$ pm2 start app.js --name my-api # Name process

# Cluster mode
$ pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs

List commands

$ pm2 list               # Display all processes status
$ pm2 jlist              # Print process list in raw JSON
$ pm2 prettylist         # Print process list in beautified JSON

$ pm2 describe 0         # Display all informations about a specific process

$ pm2 monit              # Monitor all processes

Display logs

$ pm2 logs [--raw]       # Display all processes logs in streaming
$ pm2 flush              # Empty all log file
$ pm2 reloadLogs         # Reload all logs

Actions

$ pm2 stop all           # Stop all processes
$ pm2 restart all        # Restart all processes

$ pm2 reload all         # Reload apps without downtime. Will 0s downtime reload (for NETWORKED apps)
$ pm2 gracefulReload all # Send exit message then reload (for networked apps)

$ pm2 stop 0             # Stop specific process id
$ pm2 restart 0          # Restart specific process id

$ pm2 delete 0           # Will remove process from pm2 list
$ pm2 delete all         # Will remove all processes from pm2 list

Misc Commands

$ pm2 reset <process>    # Reset meta data (restarted time...)
$ pm2 updatePM2          # Update in memory pm2
$ pm2 ping               # Ensure pm2 daemon has been launched
$ pm2 sendSignal SIGUSR2 my-app # Send system signal to script
$ pm2 start app.js --no-daemon
$ pm2 start app.js --no-vizion
$ pm2 start app.js --no-autorestart

Run apps in cluster Mode

PM2 can act as a round-robin between the main process and the 4 workers.

pm2 start app.js -i 4

Start maximum processes depending on available CPUs (cluster mode)

pm2 start app.js -i 0

Scale application

We can also scale down or up our number of workers:

pm2 scale app.js 2

We can also save the status of PM2 so that if we restart the server, and PM2 is running as a daemon, the apps will automatically start.

pstree

References


# Reference
1 PM2 Quick Start

Version History


Date Description
2015-07-01    Initial Version