Skip to content

pm2

Analyze a project and generate PM2 service commands for detected frontend, backend, or database services.


PM2 Init

Auto-analyze project and generate PM2 service commands.

Command: $ARGUMENTS


Workflow

  1. Check PM2 (install via npm install -g pm2 if missing)
  2. Scan project to identify services (frontend/backend/database)
  3. Generate config files and individual command files

Service Detection

TypeDetectionDefault Port
Vitevite.config.*5173
Next.jsnext.config.*3000
Nuxtnuxt.config.*3000
CRAreact-scripts in package.json3000
Express/Nodeserver/backend/api directory + package.json3000
FastAPI/Flaskrequirements.txt / pyproject.toml8000
Gogo.mod / main.go8080

Port Detection Priority: User specified > .env > config file > scripts args > default port


Generated Files

project/
├── ecosystem.config.cjs # PM2 config
├── {backend}/start.cjs # Python wrapper (if applicable)
└── .claude/
├── commands/
│ ├── pm2-all.md # Start all + monit
│ ├── pm2-all-stop.md # Stop all
│ ├── pm2-all-restart.md # Restart all
│ ├── pm2-{port}.md # Start single + logs
│ ├── pm2-{port}-stop.md # Stop single
│ ├── pm2-{port}-restart.md # Restart single
│ ├── pm2-logs.md # View all logs
│ └── pm2-status.md # View status
└── scripts/
├── pm2-logs-{port}.ps1 # Single service logs
└── pm2-monit.ps1 # PM2 monitor

Windows Configuration (IMPORTANT)

ecosystem.config.cjs

Must use .cjs extension

module.exports = {
apps: [
// Node.js (Vite/Next/Nuxt)
{
name: 'project-3000',
cwd: './packages/web',
script: 'node_modules/vite/bin/vite.js',
args: '--port 3000',
interpreter: 'C:/Program Files/nodejs/node.exe',
env: { NODE_ENV: 'development' }
},
// Python
{
name: 'project-8000',
cwd: './backend',
script: 'start.cjs',
interpreter: 'C:/Program Files/nodejs/node.exe',
env: { PYTHONUNBUFFERED: '1' }
}
]
}

Framework script paths:

Frameworkscriptargs
Vitenode_modules/vite/bin/vite.js--port {port}
Next.jsnode_modules/next/dist/bin/nextdev -p {port}
Nuxtnode_modules/nuxt/bin/nuxt.mjsdev --port {port}
Expresssrc/index.js or server.js-

Python Wrapper Script (start.cjs)

const { spawn } = require('child_process');
const proc = spawn('python', ['-m', 'uvicorn', 'app.main:app', '--host', '0.0.0.0', '--port', '8000', '--reload'], {
cwd: __dirname, stdio: 'inherit', windowsHide: true
});
proc.on('close', (code) => process.exit(code));

Command File Templates (Minimal Content)

pm2-all.md (Start all + monit)

Start all services and open PM2 monitor.
```bash
cd "{PROJECT_ROOT}" && pm2 start ecosystem.config.cjs && start wt.exe -d "{PROJECT_ROOT}" pwsh -NoExit -c "pm2 monit"
```

pm2-all-stop.md

Stop all services.
```bash
cd "{PROJECT_ROOT}" && pm2 stop all
```

pm2-all-restart.md

Restart all services.
```bash
cd "{PROJECT_ROOT}" && pm2 restart all
```

pm2-{port}.md (Start single + logs)

Start {name} ({port}) and open logs.
```bash
cd "{PROJECT_ROOT}" && pm2 start ecosystem.config.cjs --only {name} && start wt.exe -d "{PROJECT_ROOT}" pwsh -NoExit -c "pm2 logs {name}"
```

pm2-{port}-stop.md

Stop {name} ({port}).
```bash
cd "{PROJECT_ROOT}" && pm2 stop {name}
```

pm2-{port}-restart.md

Restart {name} ({port}).
```bash
cd "{PROJECT_ROOT}" && pm2 restart {name}
```

pm2-logs.md

View all PM2 logs.
```bash
cd "{PROJECT_ROOT}" && pm2 logs
```

pm2-status.md

View PM2 status.
```bash
cd "{PROJECT_ROOT}" && pm2 status
```

PowerShell Scripts (pm2-logs-{port}.ps1)

Terminal window
Set-Location "{PROJECT_ROOT}"
pm2 logs {name}

PowerShell Scripts (pm2-monit.ps1)

Terminal window
Set-Location "{PROJECT_ROOT}"
pm2 monit

Key Rules

  1. Config file: ecosystem.config.cjs (not .js)
  2. Node.js: Specify bin path directly + interpreter
  3. Python: Node.js wrapper script + windowsHide: true
  4. Open new window: start wt.exe -d "{path}" pwsh -NoExit -c "command"
  5. Minimal content: Each command file has only 1-2 lines description + bash block
  6. Direct execution: No AI parsing needed, just run the bash command

Execute

Based on $ARGUMENTS, execute init:

  1. Scan project for services
  2. Generate ecosystem.config.cjs
  3. Generate {backend}/start.cjs for Python services (if applicable)
  4. Generate command files in .claude/commands/
  5. Generate script files in .claude/scripts/
  6. Update project CLAUDE.md with PM2 info (see below)
  7. Display completion summary with terminal commands

Post-Init: Update CLAUDE.md

After generating files, append PM2 section to project’s CLAUDE.md (create if not exists):

## PM2 Services
| Port | Name | Type |
|------|------|------|
| {port} | {name} | {type} |
**Terminal Commands:**
```bash
pm2 start ecosystem.config.cjs # First time
pm2 start all # After first time
pm2 stop all / pm2 restart all
pm2 start {name} / pm2 stop {name}
pm2 logs / pm2 status / pm2 monit
pm2 save # Save process list
pm2 resurrect # Restore saved list
```

Rules for CLAUDE.md update:

  • If PM2 section exists, replace it
  • If not exists, append to end
  • Keep content minimal and essential

Post-Init: Display Summary

After all files generated, output:

## PM2 Init Complete
**Services:**
| Port | Name | Type |
|------|------|------|
| {port} | {name} | {type} |
**Claude Commands:** /pm2-all, /pm2-all-stop, /pm2-{port}, /pm2-{port}-stop, /pm2-logs, /pm2-status
**Terminal Commands:**
## First time (with config file)
pm2 start ecosystem.config.cjs && pm2 save
## After first time (simplified)
pm2 start all # Start all
pm2 stop all # Stop all
pm2 restart all # Restart all
pm2 start {name} # Start single
pm2 stop {name} # Stop single
pm2 logs # View logs
pm2 monit # Monitor panel
pm2 resurrect # Restore saved processes
**Tip:** Run `pm2 save` after first start to enable simplified commands.