PM2 Process Management

PM2 is used for production process management, providing auto-restart on crashes, log rotation, and system boot startup.

Configuration Files

  • ecosystem.api.json - API server process cluster (4 instances on ports 8000-8003)
  • ecosystem.scraper.json - Scraper scheduler process

Quick Commands

# Start API cluster
pm2 start ecosystem.api.json

# Start scraper scheduler
pm2 start ecosystem.scraper.json

# Stop all
pm2 stop all

# Restart
pm2 restart all

# View logs
pm2 logs

# Monitor processes
pm2 monit

# Status
pm2 list

Log Rotation

Log rotation requires the pm2-logrotate module. Install and configure it with:

# Install pm2-logrotate module
pm2 install pm2-logrotate

# Configure rotation settings (max 10M file size, retain 7 files)
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7

# Restart logrotate module to apply settings
pm2 restart logrotate

Log rotation settings configured:

  • max size: 10M per log file
  • retain: 7 log files per process

Log files are stored in /home/paperclip/buywhere-api/logs/:

  • pm2-api-8000-out.log, pm2-api-8000-error.log
  • pm2-api-8001-out.log, pm2-api-8001-error.log
  • pm2-api-8002-out.log, pm2-api-8002-error.log
  • pm2-api-8003-out.log, pm2-api-8003-error.log
  • pm2-scraper-scheduler-out.log, pm2-scraper-scheduler-error.log

Auto-Restart Configuration

Each process is configured with:

  • autorestart: true - automatically restart on crash
  • max_restarts: 10 - maximum restart attempts before giving up
  • min_uptime: 30s - minimum uptime before considering stable
  • exp_backoff_restart_delay: 1000 - exponential backoff (1s, 2s, 4s, 8s...)

Memory Limits

  • API instances: 1G max memory per process
  • Scraper scheduler: 1G max memory

System Boot Startup

To generate the startup script for your init system:

sudo env PATH=$PATH:/usr/local/bin pm2 startup systemd -u paperclip --hp /home/paperclip

To save the current process list for automatic startup:

pm2 save

Startup Script Generation

Run as root to generate and enable the systemd service:

sudo env PATH=$PATH:/usr/local/bin /home/paperclip/buywhere-api/node_modules/pm2/bin/pm2 startup systemd -u paperclip --hp /home/paperclip

After running, PM2 will display the command to enable startup on boot. Typically:

sudo systemctl enable pm2-paperclip

Monitoring

# Real-time monitoring dashboard
pm2 monit

# Detailed status
pm2 list
pm2 info buywhere-api-8000

Graceful Shutdown

PM2 sends SIGTERM for graceful shutdown (configured via kill_timeout: 5000ms).