PM2 Process Manager: Quick Setup Guide
Invalid Date
A concise guide to setting up PM2 for process management with ecosystem configuration
PM2 Process Manager
PM2 is a production-ready process manager for Node.js applications that helps keep your apps running smoothly.
Installation
pnpm install pm2 -gResources
Configuration
Create an ecosystem configuration file at ecosystem.config.js:
module.exports = {
apps: [
{
name: "web-app",
script: "npm",
args: "start",
cwd: "/home/user/projects/web-app",
env: {
NODE_ENV: "production",
PORT: 3000,
},
},
{
name: "api-server",
script: "node",
args: "server.js",
cwd: "/home/user/projects/api",
instances: 2,
exec_mode: "cluster",
},
{
name: "worker",
script: "npm",
args: "run worker",
cwd: "/home/user/projects/background-jobs",
autorestart: true,
},
],
};Usage
Start Applications
Start all applications defined in the ecosystem file:
pm2 start ecosystem.config.jsStart a specific application:
pm2 start ecosystem.config.js --only web-appRestart Applications
Restart all applications:
pm2 restart allRestart a specific application:
pm2 restart web-appStop Applications
Stop all applications:
pm2 stop allStop a specific application:
pm2 stop web-appDelete Applications
Delete all applications from PM2:
pm2 delete allDelete a specific application:
pm2 delete web-appMonitor and Logs
View application status:
pm2 statusView logs for all applications:
pm2 logsView logs for a specific application:
pm2 logs web-appPersist Configuration
Save the current PM2 process list:
pm2 saveSet PM2 to start on system boot:
pm2 startup