Back to Blog

How PM2 Keeps Node.js Applications Online

How PM2 Keeps Node.js Applications Online
Command of the Day
pm2 status

What it does: Displays a real-time table listing all applications managed by PM2, including process status, PID, memory usage, and restart counts.

When running Node.js applications or Discord bots in production, an uncaught exception or unhandled promise rejection can cause the entire Node.js process to exit immediately. Without a process supervisor, your application remains dead until a developer manually logs in and restarts it.

PM2 (Process Manager 2) is the industry-standard production process manager for Node.js. In this guide, we explore how PM2 operates, why it is essential for bot hosting, and how to configure it on your RivinCloud VPS.


1. Core Capabilities of PM2

PM2 acts as an intelligent supervisor wrapper around your Node.js processes:

  1. Automatic Crash Recovery: If your application crashes due to a runtime error, PM2 restarts it in milliseconds.
  2. Boot Persistence: PM2 hooks into Linux systemd to restore all managed processes automatically when your VPS reboots.
  3. Log Aggregation: Captures, formats, and manages standard output (STDOUT) and error (STDERR) streams.
  4. Memory Guarding (max-memory-restart): Restarts applications if a memory leak causes RAM consumption to exceed a set threshold.
  5. Cluster Mode: Scales web applications across multiple CPU cores to maximize throughput.

2. Basic PM2 Management Commands

Start an Application

pm2 start index.js --name "discord-bot"

View Managed Process Status Table

pm2 status

Output:

┌────┬──────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┐
│ id │ name         │ mode        │ status  │ cpu     │ memory   │ uptime │ restart │ user    │
├────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┤
│ 0  │ discord-bot  │ fork        │ online  │ 0.1%    │ 42.5mb   │ 12d    │ 0    │ ubuntu    │
└────┴──────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┘

Stop, Restart, or Delete a Process

pm2 stop discord-bot
pm2 restart discord-bot
pm2 delete discord-bot

View Live Logs

pm2 logs discord-bot --lines 50

3. Configuring an ecosystem.config.js File

For complex projects, use a declarative configuration file instead of CLI arguments:

pm2 init

Edit ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'discord-bot',
    script: './index.js',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '300M',
    env: {
      NODE_ENV: 'production'
    }
  }]
};

Launch using the ecosystem file:

pm2 start ecosystem.config.js

Command of the Day: pm2 status

The pm2 status (or pm2 ls) command renders a clean summary table of all active, stopped, or errored processes managed by PM2.

Syntax

pm2 status

Practical Example

Use pm2 status to check if any bot has accumulated an unusually high restart count, which signals hidden runtime exceptions.


4. Enabling Auto-Start on System Boot

To ensure PM2 resurrects your bots after a server reboot:

# 1. Save current active process snapshot
pm2 save

# 2. Generate systemd startup hook
pm2 startup

(Copy and paste the output string provided by pm2 startup into your terminal to finalize systemd integration).


Common Mistakes

  • Enabling watch: true in production: Setting watch: true causes PM2 to restart your app every time a log file or local JSON file is modified, causing infinite restart loops.
  • Forgetting pm2 save: PM2 will not restore applications on reboot unless you explicitly run pm2 save.

Troubleshooting

  • Process stuck in errored loop: Run pm2 logs --err to inspect the exact error traceback preventing startup.

Best Practices

  • Install pm2-logrotate to prevent log files from growing endlessly:
    pm2 install pm2-logrotate
    
  • Use max_memory_restart: '250M' for Discord bots to automatically clean up potential memory leaks.

Frequently Asked Questions

Can PM2 manage Python or Go applications?

Yes! PM2 supports non-Node processes by specifying an interpreter flag: pm2 start bot.py --interpreter python3


Conclusion

PM2 provides essential process supervision for production Node.js applications. Keep your bots online 24/7 on RivinCloud VPS with PM2!