ScriptsApr 25, 2026·3 min read

PM2 — Production Process Manager for Node.js

PM2 is a daemon-based process manager for Node.js applications with built-in load balancing, log management, and zero-downtime reloads.

Introduction

PM2 is a production-grade process manager that keeps Node.js applications alive, restarts them on failure, and provides cluster mode for utilizing all CPU cores. It is one of the most widely deployed Node.js operations tools.

What PM2 Does

  • Daemonizes Node.js processes so they survive terminal closure
  • Cluster mode forks the app across all CPU cores with built-in load balancing
  • Auto-restarts crashed processes with configurable restart strategies
  • Aggregates and rotates application log files
  • Provides a real-time monitoring dashboard via pm2 monit

Architecture Overview

PM2 runs a background daemon (God process) that spawns and watches child processes. In cluster mode it leverages Node.js's built-in cluster module to fork workers. The daemon communicates with the CLI over a local RPC socket. An ecosystem file (ecosystem.config.js) declares applications, environment variables, and deploy targets.

Self-Hosting & Configuration

  • Install globally via npm install -g pm2
  • Create ecosystem.config.js to declare apps, env vars, and cluster settings
  • Use pm2 startup to generate an OS init script (systemd, launchd, etc.)
  • Run pm2 save to persist the process list across reboots
  • Configure log rotation with pm2 install pm2-logrotate

Key Features

  • Zero-downtime reloads with pm2 reload for graceful restarts
  • Cluster mode distributes traffic across CPU cores without code changes
  • Built-in deploy system for pushing code to remote servers via SSH
  • Module system for extending PM2 with plugins (log rotate, monitoring)
  • Container-friendly mode (pm2-runtime) for Docker entry points

Comparison with Similar Tools

  • nodemon — development auto-restart tool; PM2 is designed for production with daemonization and clustering
  • forever — simple daemon runner; PM2 adds cluster mode, deploy, and monitoring
  • systemd — OS-level service manager; PM2 adds Node-specific features like cluster mode and ecosystem files
  • Docker — container runtime that manages process lifecycle; PM2 can run inside Docker for Node-specific clustering
  • Supervisor — Python-based process control; PM2 is purpose-built for the Node.js ecosystem

FAQ

Q: Can PM2 manage non-Node.js processes? A: Yes. PM2 can manage any executable (Python scripts, binaries, etc.), though cluster mode is Node-specific.

Q: How does PM2 handle memory leaks? A: Set max_memory_restart in the ecosystem file to auto-restart a process when it exceeds a memory threshold.

Q: Does PM2 work in Docker containers? A: Yes. Use pm2-runtime as the Docker entrypoint instead of pm2 start to keep the container foreground process alive.

Q: Is PM2 free for production use? A: The open-source CLI is free under AGPL-3.0. PM2 Plus is a paid SaaS dashboard for remote monitoring and diagnostics.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets