Posted in

PM2 Cheatsheet: The Ultimate Guide for Node.js Process Management πŸš€

Hi everyone, TM here! πŸ‘‹

Today I’m sharing a complete PM2 Cheatsheet that I personally use for managing Node.js apps in production.

Unlike traditional setups like PHP (which runs behind Apache or Nginx) or Python (which often uses servers like Gunicorn or uWSGI), Node.js apps don’t come with a built-in process manager.

You have to handle starting, restarting, and keeping your app alive manually β€” unless you use a tool like PM2.

PM2 fills that gap perfectly.

It keeps your Node.js apps running 24/7, auto-restarts after crashes, helps with scaling across CPU cores, and makes deployment super clean.

This cheatsheet covers everything you need β€” from basic commands to startup scripts and ecosystem files β€” in a simple, developer-friendly format.

Let’s dive in!

PM2 is a powerful production process manager for Node.js applications. Whether you’re running a single app or managing a fleet of services, PM2 gives you the tools to keep your code running smoothly, monitor performance, and reboot on failure β€” all with just a few commands.

πŸ”§ Installation

npm install -g pm2

βš™οΈ Basic Commands

CommandDescription
pm2 start app.jsStart an app
pm2 listList all running apps
pm2 stop <name>Stop an app
pm2 restart <name>Restart an app
pm2 delete <name>Remove an app from PM2
pm2 monitMonitor live app performance

πŸš€ Start with Options

pm2 start app.js --name myapp         # Custom name
pm2 start app.js -i max               # Cluster mode (multi-core)
pm2 start app.js --watch              # Auto-restart on file changes
pm2 start app.js --ignore-watch="node_modules logs"  # Ignore folders

πŸ›  Process Management

CommandPurpose
pm2 reload allGraceful restart all
pm2 gracefulReload <name>Graceful single restart
pm2 restart allHard restart all apps
pm2 delete allRemove all from PM2 list

πŸ” Auto-Start on Reboot

pm2 startup     # Generate startup script
pm2 save        # Save current process list

To remove startup script:

pm2 unstartup

πŸ“ Ecosystem File (for Multi-App Management)

Create ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: 'myapp',
      script: './app.js',
      watch: true,
      instances: 'max',
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'development'
      },
      env_production: {
        NODE_ENV: 'production'
      }
    }
  ]
};

Launch with:

pm2 start ecosystem.config.js

πŸ“œ Logs & Debugging

pm2 logs              # View all logs
pm2 logs &lt;name>       # View logs for specific app
pm2 flush             # Clear logs
pm2 reloadLogs        # Reload logs

Logs location: ~/.pm2/logs/


🧠 PM2 Fun Facts

  • PM2 stands for Process Manager 2.
  • Works with any language (with interpreter defined).
  • Runs in the background as a daemon.
  • Built in Node.js, supports real-time monitoring.
  • Supports JSON config for scalable deployment.

πŸ—Ί Bonus: Visual Map

Want a bird’s-eye view? Here’s a mind map image to understand the full PM2 workflow at a glance.


βœ… Conclusion

PM2 is your secret weapon for keeping your Node.js apps alive and optimized in production. Whether you’re deploying a single app or managing a fleet of services, it makes process management a breeze.

Save this cheatsheet, and run your apps like a pro.

Leave a Reply

Your email address will not be published. Required fields are marked *