0%
PM2: The Superhero Sidekick for Your Node.js Apps (Auto-Restarts & Zero-Downtime)

PM2: The Superhero Sidekick for Your Node.js Apps (Auto-Restarts & Zero-Downtime)

Tired of Node apps crashing? Meet PM2 - the guardian angel that keeps your apps running 24/7 with magical powers like auto-restarts, easy scaling, and zero-downtime deployments!

Saransh Pachhai
Saransh Pachhai
4 min read56 viewsJanuary 1, 2026
nodejspm2devopsbackendwebdevelopment
Share:

What the Heck is PM2 (And Why Should You Care)?

Picture this: You've built an awesome Node.js app. You run it with node app.js, high-five your cat, and call it a day. Next morning... "Connection refused". Your app crashed overnight!

Enter PM2 โ€“ the trusty sidekick that:

  • ๐Ÿฆธ Keeps your app alive forever (auto-restarts if it crashes)

  • โšก Makes scaling apps as easy as snapping fingers

  • ๐Ÿ” Gives you X-ray vision into your app's performance

  • โฐ Enables zero-downtime deployments

Getting Started: PM2 in 60 Seconds

Install PM2 globally (you'll only do this once):

Bash

npm install pm2 -g

Now start your Node app like a pro:

Bash

pm2 start app.js

Boom! Your app is now:

  • ๐Ÿ›ก๏ธ Protected from crashes (PM2 auto-restarts it)

  • ๐Ÿ“Š Logged (view logs with pm2 logs)

  • ๐Ÿ”ง Managed (stop/restart easily)

Local Setup (On Your Computer โ€“ Perfect for Development)

  1. Install PM2 (if not done):

    Bash

    npm install pm2 -g
  2. Go to your project folder:

    Bash

    cd your-project-folder
  3. Start your app:

    Bash

    pm2 start dist/main.js --name my-app  # or app.js
  4. Check status and logs:

    Bash

    pm2 status
    pm2 logs my-app

A Complete Guide to Node.js Process Management with PM2 ...

blog.appsignal.com

pm2 status sometimes does not show the details like restart ...

stackoverflow.com

Production Setup (Real Server โ€“ With SSH)

In real projects, your app runs on a remote server.

  1. Connect via SSH (from your laptop terminal):

    Bash

    ssh pachhai@192.168.1.100  # replace with your username & IP

    (First time? Type "yes" to accept.)

Windows Terminal and SSH - the most beautiful SSH client? | Nicola ...

tech.nicolonsky.ch

Connecting to your Server using SSH - Documentation | Bytemark

docs.bytemark.co.uk

  1. Go to backend folder (common pattern: project-name โ†’ backend):

    Bash

    cd myproject/backend
  2. Install PM2 (only once on server):

    Bash

    npm install pm2 -g

Real-World PM2 Usage: From Development to Production

In actual workflows (like my recent project), PM2 manages different environments safely.

Multi-Environment Management

Check running processes:

Bash

pm2 status

Typical output (dev for testing, prod for live users):

A Complete Guide to Node.js Process Management with PM2 ...

blog.appsignal.com

A Complete Guide to Node.js Process Management with PM2 ...

blog.appsignal.com

pm2 status sometimes does not show the details like restart ...

stackoverflow.com

Critical Rule: Always work on dev (usually higher ID) and never touch prod (usually ID 0) during development. This prevents downtime!

The Development Workflow

  1. After code changes, build:

    Bash

    yarn build  # or npm run build
  2. Reload dev:

    Bash

    pm2 reload dev  # or pm2 reload 1
  3. Monitor logs:

    Bash

    pm2 logs dev  # or pm2 logs 1

node.js - Why pm2 is fine, but pm2 logs is not? - Stack Overflow

stackoverflow.com

pm2 fatally crashes on ENOMEM when stopping process ยท Issue #4240 ...

github.com

When ready, deploy to prod:

Bash

pm2 reload prod

Live Monitoring Dashboard

Bash

pm2 monit

PM2 - App Dashboard | Guide | PM2 Plus Documentation

pm2.io

PM2 - Monitoring

pm2.keymetrics.io

PM2 Cheat Sheet: Must-Know Commands

Bash

pm2 list              # Same as status
pm2 stop app_name
pm2 restart app_name
pm2 reload app_name   # Zero downtime!
pm2 logs app_name --lines 100
pm2 monit
pm2 save              # Save processes
pm2 show app_name

Supercharge Your Setup: Pro Tips

1. The Magic Configuration File

Create ecosystem.config.js for pro setups:

node.js - dockerizing a nodejs application - should pm2 ls work ...

stackoverflow.com

node.js - dockerizing a nodejs application - should pm2 ls work ...

JavaScript

module.exports = {
  apps: [{
    name: "My Awesome App",
    script: "./dist/main.js",
    instances: 2,
    autorestart: true,
    watch: false,
    max_memory_restart: "1G",
    env: { NODE_ENV: "development", PORT: 3000 },
    env_production: { NODE_ENV: "production", PORT: 80 }
  }]
};

Start:

Bash

pm2 start ecosystem.config.js --env production

2. Log Management for Debugging

Bash

pm2 logs --lines 50
pm2 logs --raw
pm2 logs --err
pm2 logs --out
pm2 logs --timestamp

3. Startup Scripts (Survive Reboots!)

Bash

pm2 startup
pm2 save

Real-World PM2 Superpowers in Action

Scenario 1: 10x Traffic

Bash

pm2 scale app_name 4

Scenario 2: Zero-Downtime Update

Bash

git pull origin main
npm install
npm run build
pm2 reload app_name

Scenario 3: Debugging Issues

Bash

pm2 status
pm2 logs app_name --err --lines 100
pm2 monit
pm2 reload app_name

Scenario 4: Multiple Environments

Bash

pm2 start ecosystem.config.js --env development
pm2 start ecosystem.config.js --env production --name "app-prod"
pm2 list
pm2 reload app-dev

Your PM2 Action Plan

  1. Install today: npm i pm2 -g

  2. Replace node app.js with PM2

  3. Create ecosystem.config.js

  4. Try pm2 monit

  5. Run pm2 startup && pm2 save

Lessons from the Trenches

  • Always reload, not restart

  • Check logs after every deploy

  • Separate dev/prod

  • Run pm2 save on warnings

  • Watch memory leaks!

PM2 isn't just a tool โ€“ it's your app's insurance policy. Happy coding, and may your servers stay forever online! ๐Ÿš€

Loading comments...

Designed & developed withโค๏ธbySaransh Pachhai

ยฉ2026. All rights reserved.