Node ~ PM2 - rohit120582sharma/Documentation GitHub Wiki

PM2 is a daemon process manager for Node.js applications with a built-in load balancer. It helps to manage and keep the application online, to reload them without downtime and to facilitate common system admin tasks.

PM2 has everything to do with spawning multiple instances of node app and managing the health of every single one of them.

PM2 has many features including:

  • Auto restart an app, if there is any change in code with Watch & Reload.
  • An easy log management for processes.
  • Monitoring capabilities of the process.
  • An auto restart if the system reaches max memory limit or it crashes.

References


Getting Start

Install

$ npm install -g pm2

Start an application

$ pm2 start app.js

Managing Applications

To list all running applications:

$ pm2 list

Managing apps is straightforward:

$ pm2 stop <app_name|namespace|id|'all'|json_conf>
$ pm2 restart <app_name|namespace|id|'all'|json_conf>
$ pm2 delete <app_name|namespace|id|'all'|json_conf>

To monitor logs, custom metrics, application information:

$ pm2 monit

Cluster mode

PM2 includes an automatic load balancer that will share all HTTP[s]/Websocket/TCP/UDP connections between each spawned processes.

The cluster mode allows Node.js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available. Under the hood, this uses the Node.js cluster module such that the scaled application’s child processes can automatically share server ports.

To start an application in Cluster mode:

$ pm2 start app.js -i 0

⚠️ **GitHub.com Fallback** ⚠️