P(rocess) M(anager) 2
Runtime Edition
PM2 Runtime is a Production Process Manager for Node.js applications with a built-in Load Balancer. It allows you to keep applications alive forever, to reload them without downtime and facilitate common Devops tasks.
Starting an application in production mode is as easy as:
$ pm2 start app.js
PM2 is constantly assailed by more than 1800 tests.
Works on Linux (stable) & macOS (stable) & Windows (stable). All Node.js versions are supported starting Node.js 4.X.
One command to production
PM2 will keep your application forever alive, auto-restarting across crashes and machine restarts.
Starting your application in production mode is as easy as:
pm2 start app.js
And to restarts accross machine restarts just type:
pm2 startup
Intuitive Process Management
All your applications are run in the background and can be easily managed.
PM2 creates a list of processes, that you can access with:
pm2 ls
You will then see the result in your terminal:
Add and delete processes to your process list with pm2 start
and pm2 delete
.
Manage your processes with pm2 start
, pm2 stop
, pm2 restart
, pm2 reload
.
Simple Log Management
PM2 will automatically hook to your application to manage logs easily. All logs will be stored into the folder ~/.pm2/logs
and can be accessed easily, in realtime with:
pm2 logs <app_name|all>
When logs files become too large, log rotation is a must have, that’s why there is the module pm2-logrotate that takes cares of rotating all logs automatically:
pm2 install pm2-logrotate
Cluster Mode: Load Balancing & 0s Downtime
PM2 can scale up your application accross all CPUs available by creating several child processes that share the same server port. It works great for HTTP/TCP/UDP and can increase performance by a factor of x10 on 16 core machines. It also suppress downtimes on updates, with built-in zero-downtime reloads.
Starting a Node.js application in cluster mode that will leverage all CPUs available is as easy as:
pm2 start api.js -i max
Then to update application without downtimes:
pm2 reload all
Cluster mode is seamlessly supported by all major Node.js frameworks.
The Ecosystem: Behavioral Application Configuration
When deploying on multiple servers or when using multiple CLI arguments, an alternative to the command line becomes more conveninent for starting your apps.
The purpose of the ecosystem file is to gather all options and environment variables for all your applications.
module.exports = {
apps : [{
name: "main-api",
script: "./api.js",
mode: "cluster",
instances: 4,
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}, {
name: "worker",
script: "./worker.js"
}]
}
In-terminal monitoring
You can even monitor your application directly in the terminal and check app health (CPU usage, memory used, request/min and more) with the command:
pm2 monit
Deployment System
PM2 includes a simple but powerful deployment system that can update your application accross several machines
pm2 deploy production