PM2

Process Management

Manage Node.js applications with PM2

PM2 Process Management

PM2 is an advanced Node.js process manager for production applications. SkyPort provides a seamless CLI interface for PM2 management.

Starting Applications

Basic Start

# Start a Node.js application
skyport pm2 start app.js --name "my-app"

# Start with custom interpreter
skyport pm2 start app.py --interpreter python --name "python-app"

# Start with npm start
skyport pm2 start "npm start" --name "my-app"

Ecosystem File

For more complex configurations, use an ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "my-app",
      script: "server.js",
      instances: 2,
      exec_mode: "cluster",
      env: {
        NODE_ENV: "development",
        PORT: 3000,
      },
      env_production: {
        NODE_ENV: "production",
      },
      error_file: "./logs/error.log",
      out_file: "./logs/out.log",
      log_date_format: "YYYY-MM-DD HH:mm:ss Z",
      merge_logs: true,
    },
    {
      name: "worker",
      script: "worker.js",
      instances: 1,
      exec_mode: "fork",
      watch: ["worker.js"],
    },
  ],
};

Start with ecosystem file:

skyport pm2 start ecosystem.config.js
skyport pm2 start ecosystem.config.js --env production

Process Management

List Processes

# View all processes
skyport pm2 list

# View detailed process info
skyport pm2 describe app-name

# View in JSON format
skyport pm2 list --format json

Control Processes

# Start process
skyport pm2 start app.js --name "app"

# Stop process
skyport pm2 stop app-name

# Restart process
skyport pm2 restart app-name

# Delete process
skyport pm2 delete app-name

# Restart all processes
skyport pm2 restart all

# Stop all processes
skyport pm2 stop all

# Delete all processes
skyport pm2 delete all

Logs

View Logs

# View logs
skyport pm2 logs app-name

# Follow logs (tail -f style)
skyport pm2 logs app-name -f

# View last 100 lines
skyport pm2 logs app-name --lines 100

# View error logs
skyport pm2 logs app-name --err

# View all logs
skyport pm2 logs

# Clear logs
skyport pm2 flush

Log Rotation

Configure in ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "app",
      script: "server.js",
      instances: 1,
      error_file: "./logs/error.log",
      out_file: "./logs/out.log",
      log_file: "./logs/combined.log",
      log_date_format: "YYYY-MM-DD HH:mm:ss Z",
      log_size: "10M",
      compress: true,
      rotate_module: true,
    },
  ],
};

Clustering & Scaling

Cluster Mode

# Start in cluster mode (multiple instances)
skyport pm2 start app.js -i max --name "app"

# Start with specific number of instances
skyport pm2 start app.js -i 4 --name "app"

# View cluster status
skyport pm2 list

Scaling

# Scale up
skyport pm2 scale app-name 4

# Scale down
skyport pm2 scale app-name 2

# View current instances
skyport pm2 describe app-name

Monitoring

View Statistics

# View process statistics
skyport pm2 describe app-name

# Monit dashboard (if available)
skyport pm2 monit

Memory and CPU

# Monitor all processes
skyport pm2 monit

# View in table format
skyport pm2 list

# Get detailed metrics
skyport pm2 describe app-name | grep -E "memory|cpu"

Auto-Restart & Persistence

Auto-Start on System Boot

# Generate startup script
skyport pm2 startup

# Save current process list
skyport pm2 save

# Resurrect processes after system reboot
skyport pm2 resurrect

# Disable auto-start
skyport pm2 unstartup

Watch Files

Watch for file changes and automatically restart:

In ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "app",
      script: "server.js",
      watch: ["src"],
      ignore_watch: ["node_modules", "logs", ".git"],
      watch_delay: 1000,
    },
  ],
};

Or via CLI:

skyport pm2 start app.js --watch

Environment Variables

Per-Environment Configuration

module.exports = {
  apps: [
    {
      name: "app",
      script: "server.js",
      env: {
        NODE_ENV: "development",
        PORT: 3000,
        DEBUG: "true",
      },
      env_production: {
        NODE_ENV: "production",
        PORT: 8080,
        DEBUG: "false",
      },
    },
  ],
};

Start with environment:

skyport pm2 start ecosystem.config.js --env production

Runtime Environment Variables

# Set environment variable
skyport pm2 start app.js --name "app" --env NODE_ENV=production

# Or use environment file
export NODE_ENV=production
skyport pm2 start app.js --name "app"

Advanced Features

Graceful Reload (Zero-Downtime)

# Reload without stopping service
skyport pm2 reload app-name

# Reload all
skyport pm2 reload all

Handle Signals

// In your application
process.on('SIGTERM', () => {
  console.log('SIGTERM received, shutting down gracefully...');
  server.close(() => {
    process.exit(0);
  });
});

Max Memory Restart

In ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "app",
      script: "server.js",
      max_memory_restart: "500M", // Restart if exceeds 500MB
    },
  ],
};

Troubleshooting

Process Won't Start

# Check error logs
skyport pm2 logs app-name --err

# Try running directly
node app.js

# Check dependencies
npm install

High Memory Usage

# View memory statistics
skyport pm2 describe app-name

# Enable memory restart
skyport pm2 start app.js --max-memory-restart 512M --name "app"

# Profile memory usage
node --inspect app.js

CPU Usage at 100%

# Check what's running
skyport pm2 list

# View detailed stats
skyport pm2 monit

# Look for infinite loops or bugs
skyport pm2 logs app-name -f

Next: Zero-Downtime Deployments | Logs

SkyPort

SkyPort Docs

Self-hosted infrastructure platform