Lesson 36 +10 XP

Production Process Management with PM2

Keeping Node Servers Alive in Production

In production environments, Node.js applications must run continuously, restart automatically on crashes, and scale across CPU cores.

What is PM2?

PM2 is a production process manager for Node.js applications with a built-in load balancer.

Essential PM2 CLI Commands

# Install PM2 globally
npm install -g pm2

# Start server process with a custom name
pm2 start server.js --name "express-api"

# Scale across all available CPU cores using Cluster Mode
pm2 start server.js -i max --name "express-cluster"

# View running process status list
pm2 list

# Monitor CPU & Memory usage in real time
pm2 monit

# View aggregated logs
pm2 logs

# Restart or Stop process
pm2 restart express-api
pm2 stop express-api

# Save PM2 process list to restore on system reboot
pm2 save
pm2 startup

TL;DR

  • PM2 automatically restarts failed Node.js applications.
  • pm2 start server.js -i max runs Node in Cluster Mode across all CPU cores.
  • pm2 startup ensures server process auto-launches on server reboot.