home / skills / openclaw / skills / pm2

This skill helps you manage Node.js applications with PM2, enabling deployment, monitoring, auto-restarts, and process lifecycle control across environments.

npx playbooks add skill openclaw/skills --skill pm2

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
2.1 KB
---
name: pm2
description: Manage Node.js applications with PM2 process manager. Use for deploying, monitoring, and auto-restarting Node apps in production. Covers starting apps, viewing logs, setting up auto-start on boot, and managing multiple processes.
---

# PM2 Process Manager

Production process manager for Node.js with built-in load balancer.

## Install

```bash
npm install -g pm2
```

## Quick Start

```bash
# Start an app
pm2 start app.js
pm2 start npm --name "my-app" -- start
pm2 start "npm run start" --name my-app

# With specific port/env
pm2 start npm --name "my-app" -- start -- --port 3000
PORT=3000 pm2 start npm --name "my-app" -- start
```

## Common Commands

```bash
# List processes
pm2 list
pm2 ls

# Logs
pm2 logs              # All logs
pm2 logs my-app       # Specific app
pm2 logs --lines 100  # Last 100 lines

# Control
pm2 restart my-app
pm2 stop my-app
pm2 delete my-app
pm2 reload my-app     # Zero-downtime reload

# Info
pm2 show my-app
pm2 monit             # Real-time monitor
```

## Auto-Start on Boot

```bash
# Save current process list
pm2 save

# Generate startup script (run the output command with sudo)
pm2 startup

# Example output - run this:
# sudo env PATH=$PATH:/opt/homebrew/bin pm2 startup launchd -u username --hp /Users/username
```

## Next.js / Production Builds

```bash
# Build first
npm run build

# Start production server
pm2 start npm --name "my-app" -- start

# Or with ecosystem file
pm2 start ecosystem.config.js
```

## Ecosystem File (ecosystem.config.js)

```javascript
module.exports = {
  apps: [{
    name: 'my-app',
    script: 'npm',
    args: 'start',
    cwd: '/path/to/app',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    }
  }]
}
```

## Useful Flags

| Flag | Description |
|------|-------------|
| `--name` | Process name |
| `--watch` | Restart on file changes |
| `-i max` | Cluster mode (all CPUs) |
| `--max-memory-restart 200M` | Auto-restart on memory limit |
| `--cron "0 * * * *"` | Scheduled restart |

## Cleanup

```bash
pm2 delete all        # Remove all processes
pm2 kill              # Kill PM2 daemon
pm2 unstartup         # Remove startup script
```

Overview

This skill manages Node.js applications using PM2, a production-ready process manager with clustering and process monitoring. It helps deploy, monitor, and auto-restart apps, and supports log viewing, zero-downtime reloads, and boot autostart. The skill covers starting apps, using ecosystem files, and common operational commands for production environments.

How this skill works

The skill issues PM2 CLI commands to start and control Node processes, or uses an ecosystem.config.js file for grouped app definitions. It inspects process status, logs, memory/CPU usage, and enables features like clustering, file-watch restarts, and scheduled restarts. It also provides steps to save process lists and generate startup scripts so PM2 resumes processes after reboots.

When to use it

  • Deploying Node.js apps to production where reliability and auto-recovery are required.
  • Running multiple processes or leveraging cluster mode across CPUs.
  • Needing centralized logs, monitoring, and zero-downtime reloads.
  • Automating app startup on server boot and maintaining process state.
  • Managing memory or scheduled restarts to reduce leaks and downtime.

Best practices

  • Use an ecosystem.config.js to define apps, environments, and working directories for repeatable deployments.
  • Run pm2 save after starting processes and follow pm2 startup output to enable autostart on boot.
  • Prefer pm2 reload for zero-downtime updates and pm2 restart for hard restarts when necessary.
  • Set --max-memory-restart and health checks to auto-recover from memory leaks.
  • Keep logs rotated and review pm2 logs --lines N for recent context before taking action.

Example use cases

  • Start a production Next.js app: build with npm run build then pm2 start npm --name my-app -- start.
  • Run an app in cluster mode to use all CPUs: pm2 start app.js -i max.
  • Schedule weekly restarts to release leaked memory: pm2 start app.js --cron "0 3 * * 0".
  • Deploy multiple services from an ecosystem file: pm2 start ecosystem.config.js.
  • Enable auto-restart on system boot: pm2 save then run the pm2 startup command output with sudo.

FAQ

How do I view logs for a specific app?

Use pm2 logs <app-name> or pm2 logs <id> and add --lines N to view recent lines.

How do I ensure PM2 restarts apps after a server reboot?

Run pm2 save to persist the current process list, then run pm2 startup and execute the printed sudo command to install the startup script.