PM2 Cheatsheet - Node.js Process Manager Reference

Running node app.js in the foreground means watching a process die or a memory leak build with no safety net. PM2 layers on daemon supervision, self-restart, cluster mode, logging and boot-start as one loop. This table is grouped into start/run, logs, monitoring, cluster, startup and ecosystem.config.js, with deployment-critical flags, and it calls out CLI differences like reload vs restart and the pm2 save + startup pair for crash recovery. After reading you can set up supervision, scaling and self-healing for a Node service on your own.

Languages·38 commands·Last updated 2026-07-21
pm2nodeProcess ManagementClusters

Start & Stop 12

pm2 start app.js
Start an app
pm2 start app.js --name "api"
Start and name the process
pm2 start app.js -i max
Cluster mode, one per CPU core
pm2 start app.js -i 4
Cluster mode, 4 instances
pm2 start ecosystem.config.js
Start from a config file (recommended)
pm2 stop all
Stop all processes
pm2 stop api
Stop a named process
pm2 restart api
Restart (reload code)
pm2 reload api
Zero-downtime reload (cluster)
pm2 delete api
Delete from process list
pm2 start app.js --watch
Watch files and auto-restart
pm2 kill
Kill all and stop the daemon

Logs 5

pm2 logs
Stream all logs live
pm2 logs api --lines 100
Last 100 lines of a process
pm2 logs api --err
Errors only
pm2 flush
Flush all log files
pm2 install pm2-logrotate
Install logrotate to cap log growth

Monitor & Status 6

pm2 list
List all processes
pm2 show api
Show process details
pm2 monit
Terminal monitor (CPU/mem)
pm2 status
Show process status
pm2 describe api
Describe a process
pm2 jlist
JSON list (script-friendly)

Startup 4

pm2 startup
Generate startup script, run the printed command
pm2 save
Save process list for startup
pm2 resurrect
Restore saved process list
pm2 unstartup
Remove startup

Cluster & Scale 5

pm2 scale api +2
Scale up by 2
pm2 scale api 8
Set total instances to 8
pm2 reset api
Reset restart count
pm2 serve ./dist 8080
Serve a static directory
pm2 deploy ecosystem.config.js production
Deploy to remote via config

ecosystem.config.js 6

module.exports = { apps: [{ name: "api", script: "app.js", instances: "max", exec_mode: "cluster" }] }
Basic cluster config
env: { NODE_ENV: "production", PORT: 3000 }
Environment variables
max_memory_restart: "1G"
Auto-restart over 1G memory
error_file: "./logs/err.log", out_file: "./logs/out.log"
Custom log paths
watch: true, ignore_watch: ["node_modules", "logs"]
Auto-restart on file change
pm2 start ecosystem.config.js --env production
Start with a named env

Typical Use Case

Before a Node service goes live, start it with -i max to spawn one process per CPU core, then centralize tuning in ecosystem.config.js; when a process crashes or leaks memory, watch trends with pm2 monit and set max_memory_restart to auto-restart over-limit processes; to release without dropping online users use reload for zero-downtime restarts, especially for stateful or long-lived services; to bring everything back after a reboot combine pm2 save with pm2 startup; for daily triage run pm2 list to find non-online processes, then pm2 logs for stack traces.

Command Examples

Start in cluster mode and confirm it is running

pm2 start app.js -i max pm2 list

-i max 按 CPU 核数启动;pm2 list 看 status 是否 online、instances 是否等于核数,restart:0 表示未发生过崩溃重启。

Output

│ api   │ online │ 8 │ 4.2MB │ restart:0 │

Zero-downtime rolling reload after a config change

pm2 reload api

集群模式下 reload 会逐个重启进程实现零停机;若应用为 fork(单进程)模式,reload 与 restart 效果相同、无法零停机。

Enable log rotation to avoid filling the disk

pm2 install pm2-logrotate pm2 set pm2-logrotate:max_size 10M

日志无限增长会撑爆磁盘;安装后调整 max_size(默认 10M)与默认每天轮转,旧日志按保留策略清除。

Output

Modifying value of setting [pm2-logrotate:max_size] with value 10M

Common Pitfalls

  • pm2 kill exits the daemon and clears processes — never use kill to "restart PM2" in production; restart the app or the host instead.
  • pm2 startup only prints a command; you must actually run that sudo/root line once for boot-start to take effect — a step many people skip.
  • reload is zero-downtime only in cluster mode; in fork mode it behaves like restart and drops connections.
  • --watch can treat node_modules or logs as restart triggers; always set ignore_watch to avoid useless restart churn.
  • pm2 monit is a live panel that stops sampling when you exit; for sustained memory/restart trends use monitoring/alerting rather than watching a terminal.

Tips

  • reload is smoother than restart; in cluster mode it restarts one by one (zero downtime); in fork mode reload == restart.
  • pm2 save + pm2 startup restores all processes after a reboot.
  • For leaks: watch pm2 monit; set max_memory_restart as a safety net.

FAQ

What is the difference between pm2 reload and restart?

restart kills the process and starts it again with a brief downtime; reload in cluster mode cycles workers one by one for zero-downtime updates. Use pm2 reload app for smooth deploys with cluster mode, and restart when you just want to reset a single instance.

pm2 keeps failing with port already in use after restarts — what do I do?

Usually an old process was not fully cleaned up or the port is held elsewhere. Run pm2 delete app to remove the old instance, inspect the port (ss -tlnp on Linux) and kill the holder, verify PORT is free, then pm2 start again. A crash-restart loop can look the same — check pm2 logs for the real exception.

How do I configure pm2 to start on system boot?

Start the apps to persist with pm2 start, run pm2 save to store the list, then run pm2 startup and execute the init command it prints (it generates a systemd or sysv unit). On the next boot PM2 restores the saved apps automatically.

Does pm2 auto-restart crashed processes, and how do I cap memory usage?

A normal crash is restarted by default; set max_restarts to bound repeated retries. To stop a memory leak from exhausting RAM, set --max-memory-restart 1G on start or max_memory_restart in ecosystem.config.js so pm2 restarts above the threshold. Pair it with log rotation for long-lived heavy instances.

What does pm2 save do, and what happens if I skip it?

pm2 save writes the current process list into a dump file, which is required for pm2 startup (boot persistence) and for pm2 resurrect to restore apps. Without it, a reboot or fresh machine loses the managed list and you must pm2 start everything again manually.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us