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 -g

Resources

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.js

Start a specific application:

pm2 start ecosystem.config.js --only web-app

Restart Applications

Restart all applications:

pm2 restart all

Restart a specific application:

pm2 restart web-app

Stop Applications

Stop all applications:

pm2 stop all

Stop a specific application:

pm2 stop web-app

Delete Applications

Delete all applications from PM2:

pm2 delete all

Delete a specific application:

pm2 delete web-app

Monitor and Logs

View application status:

pm2 status

View logs for all applications:

pm2 logs

View logs for a specific application:

pm2 logs web-app

Persist Configuration

Save the current PM2 process list:

pm2 save

Set PM2 to start on system boot:

pm2 startup