PM2 is the process supervisor most Node.js teams adopt because it understands clustering, log rotation, and zero-downtime reload out of the box. On a production host it sits between systemd and the application, and the trick is configuring it so systemd owns the lifecycle while PM2 handles per-process concerns. This article walks through that arrangement for a typical Express/Fastify service.
How to verify
Check what is currently running and how it was started:
pm2 status
pm2 logs --lines 30 --nostream
systemctl status pm2-deploy 2>/dev/null | head
sudo lsof -iTCP:3000 -sTCP:LISTEN
ls -la /home/deploy/.pm2/logs/
If systemctl status pm2-deploy returns “not loaded”, PM2 is running but is not surviving a reboot. That is the most common gap on hosts where PM2 was installed quickly and pm2 startup was skipped.
What’s happening
PM2 has two modes worth knowing. In fork mode it spawns one Node process per app entry. In cluster mode it uses Node’s built-in cluster module to spawn N workers sharing a listening socket — kernel-level connection balancing across cores, no reverse proxy required for the load distribution itself. For a CPU-bound service on an 8-vCPU host, cluster mode with instances: "max" gives you roughly 7x the throughput of single-process for free.
The other half is durability. PM2 by itself dies when the host reboots. Its pm2 startup subcommand generates a systemd unit (pm2-<user>.service) that re-launches PM2 at boot and restores the last-saved process list. That is what turns PM2 into a real production supervisor rather than a dev convenience.
The procedure
-
Install PM2 globally under the deploy user. Do not use
sudo npm install -g— install in the user’s npm prefix so updates do not require root.sudo -iu deploy npm install -g pm2@latest pm2 -v -
Create
ecosystem.config.jsat the project root. This is the source of truth for how the app is launched:module.exports = { apps: [{ name: 'api', script: './server.js', cwd: '/var/www/app', instances: 'max', exec_mode: 'cluster', max_memory_restart: '512M', env: { NODE_ENV: 'production', PORT: 3000 }, error_file: '/var/log/app/error.log', out_file: '/var/log/app/out.log', merge_logs: true, time: true, }] };max_memory_restartis the per-worker circuit breaker — if a worker leaks past 512 MiB, PM2 recycles it without affecting siblings. -
Start the app, persist the list, and generate the boot unit:
pm2 start ecosystem.config.js pm2 save pm2 startup systemd -u deploy --hp /home/deploy # the previous command prints a `sudo ...` line — run itAfter this,
systemctl status pm2-deployshould beactive (running). -
Install the log rotation module so logs do not fill the disk. PM2’s logs append forever by default — a busy API will fill 100 GB in a month.
pm2 install pm2-logrotate pm2 set pm2-logrotate:max_size 100M pm2 set pm2-logrotate:retain 14 pm2 set pm2-logrotate:compress true -
For deployments, use
pm2 reloadrather thanrestart. Reload performs a rolling drain across cluster workers — new workers come up, take traffic, then old workers drain their in-flight requests.git -C /var/www/app pull && npm ci --production pm2 reload api --update-env
Operational notes
- Cluster mode only works for stateless services. Anything holding in-memory state per process (in-memory rate limiters, session stores, SSE connection tables) needs to move to Redis or you will see inconsistent behavior across workers.
pm2 saveis not automatic. After everypm2 start,pm2 delete, orpm2 scale, runpm2 saveor the next reboot will restore an older snapshot.- Native modules (
bcrypt,sharp,better-sqlite3) often break on Node upgrades. Addnpm rebuildto the deploy script and watchpm2 logsforNODE_MODULE_VERSIONerrors after each upgrade. - The PM2 dashboard at
pm2 monitis useful interactively but hammers CPU. Do not leave it open in a forgotten tmux session. - For services that should NOT auto-restart on crash (one-shot jobs, migrations), set
autorestart: falseandinstances: 1in the ecosystem file.
For a multi-process Node deployment behind a reverse proxy, the next piece is Nginx — see Nginx as a reverse proxy for Node.js. In the engagements Stack Harbor runs, the PM2 + systemd + Nginx triad is a standard pattern for stateless Node workloads under managed operations, and we keep the deploy scripts boring so the surprises are caught by monitoring instead.