Skip to content

Process Management with PM2

Process Management with PM2

While node app.js is fine for development, production environments require a process manager to keep the application running continuously, automatically restart it upon crashes, and manage logs. PM2 is the industry standard for this.

1. Installing PM2

Install PM2 globally so the command is available anywhere on your system:

Terminal window
npm install -g pm2

2. Basic Commands

Starting an application

Terminal window
pm2 start app.js --name "my-node-api"

Listing running apps

Terminal window
pm2 list
pm2 status

Restarting / Stopping / Deleting

Terminal window
pm2 restart my-node-api
pm2 stop my-node-api
pm2 delete my-node-api

Viewing Logs

To see real-time logs for your application:

Terminal window
pm2 logs my-node-api

3. Cluster Mode

Node.js is single-threaded. PM2 can automatically scale your application across all available CPU cores using Cluster Mode, maximizing performance.

Terminal window
pm2 start app.js -i max

(The -i max flag tells PM2 to start as many instances as there are CPU cores).

4. PM2 Ecosystem File

Instead of passing long command-line arguments, you can define a configuration file.

Generate a template:

Terminal window
pm2 init

Example ecosystem.config.js:

module.exports = {
apps : [{
name : "my-api",
script : "./app.js",
instances: "max",
env_production: {
NODE_ENV: "production",
PORT: 8080
}
}]
}

Run with: pm2 start ecosystem.config.js --env production

5. Startup Script

To ensure PM2 automatically starts your Node apps when the server reboots:

Terminal window
pm2 startup
pm2 save