PM2
By Keymetrics
js applications that keeps processes running in production, automatically restarting them on crash, and provides built-in load balancing across multiple CPU cores through a cluster mode. It also offers log management, process monitoring,…
Definition
PM2 is a process manager for Node.js applications that keeps processes running in production, automatically restarting them on crash, and provides built-in load balancing across multiple CPU cores through a cluster mode. It also offers log management, process monitoring, and zero-downtime reload capabilities, all controlled through a command-line interface and configuration files, without requiring code changes to the application being managed.
Overview
PM2 addresses a basic but critical production requirement for Node.js applications: a Node process that crashes needs to be restarted automatically, and a single-threaded Node process needs help to use all the CPU cores available on a modern server. Running `node app.js` directly in production leaves both problems unsolved, so operators historically wrapped Node processes in external supervisors, and PM2 was built specifically to serve that role for the Node.js ecosystem. Mechanically, PM2 runs as a background daemon that launches and supervises one or more application processes, restarting any process that exits unexpectedly and optionally restarting processes that exceed a memory threshold. Its cluster mode uses Node's built-in `cluster` module to fork multiple instances of an application, one typically per CPU core, and load-balances incoming connections across them, which lets an otherwise single-threaded Node application use a multi-core machine without the application code needing to manage clustering itself. PM2 also aggregates logs from all managed processes, exposes real-time CPU and memory monitoring, and supports zero-downtime reloads, restarting cluster workers one at a time so the application keeps serving traffic during a deployment. Within the Node.js process-management space, PM2's main alternative is Nodemon, but the two solve different problems: Nodemon restarts a process on file changes during local development, while PM2 is a production process supervisor focused on uptime, clustering, and monitoring, and the two are commonly used together at different stages of a project's lifecycle rather than as substitutes for each other. Container-orchestration platforms like Kubernetes provide overlapping restart and scaling capabilities at the infrastructure level, which is why PM2 is used less often in fully containerized, orchestrator-managed deployments than in traditional VM or bare-metal deployments. In practice, PM2 is used to run Node.js applications on virtual machines or dedicated servers where no external orchestrator handles process supervision, to enable multi-core usage for CPU-bound or high-throughput Node services, and to perform rolling restarts during deployments without dropping incoming requests. It remains common in small-to-mid-size deployments and in teams that prefer a simpler operational model than a full container orchestration platform. The main limitation is overlap with capabilities that container orchestrators already provide: in a Kubernetes or similar environment, the orchestrator typically handles process restarts and horizontal scaling at the pod level, which can make PM2's clustering and restart features partially redundant, so teams running in that context often use PM2, if at all, only for its clustering within a single container rather than for full process supervision.
Key Features
- Automatic restart of crashed or memory-exceeding Node.js processes
- Cluster mode load-balances traffic across multiple CPU cores
- Zero-downtime reloads during deployments via rolling worker restarts
- Aggregated log collection across all managed processes
- Real-time CPU and memory monitoring from the command line or dashboard
- Configuration via simple CLI commands or an ecosystem config file