PM2 Guide

Installation

PM2 is a process manager for Node.js applications. Install it globally using npm to access PM2 commands from anywhere in your terminal.

npm install -g pm2

Basic Tab Commands

These commands help you manage individual processes/tabs in PM2. Each process can be identified by its name for easy management.

# Create new tab
pm2 start "npm run dev" --name dev-tab

# List all tabs
pm2 list

# Stop tab
pm2 stop dev-tab

# Restart tab
pm2 restart dev-tab

# Delete tab
pm2 delete dev-tab

Multi-Tab Configuration

Create an ecosystem file to manage multiple processes in one configuration. This approach is recommended for projects with multiple services or environments.

Create ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: 'dev-tab',
      script: 'npm',
      args: 'run dev',
      autorestart: true,
      watch: false
    },
    {
      name: 'build-tab',
      script: 'npm',
      args: 'run build',
      autorestart: true,
      watch: false
    }
  ]
}

Launch Multi-Tab

Commands to start and persist your configured processes. The save and resurrect commands ensure your processes restart automatically after system reboots.

pm2 start ecosystem.config.js

# Save tab configuration
pm2 save

# Restore tabs after system restart
pm2 resurrect

Auto-Start on System Boot

Set up PM2 to automatically start your applications when the system reboots:

# Generate startup script
pm2 startup

# Save current process list
pm2 save

# Remove startup script
pm2 unstartup

For Ubuntu/Debian systems, PM2 will automatically detect and use systemd for startup management. For other systems, PM2 will generate appropriate startup scripts.

Monitoring

Monitor your running processes for debugging and performance optimization. These commands provide real-time information about your applications.

# View logs
pm2 logs

# Monitor CPU/Memory
pm2 monit