PM2: The Ultimate Process Manager for Node.js Applications

Shubham SinghShubham Singh
6 min read

When deploying Node.js applications to production, developers often face a recurring set of challenges — apps crashing unexpectedly, losing logs, restarting servers manually, or dealing with system reboots that disrupt uptime. These issues can become major blockers for scalability, maintainability, and reliability.

This is where PM2 (Process Manager 2) comes into play — a powerful, production-grade process manager that can help you keep your Node.js applications alive, optimized, and easy to maintain.

In this blog, we’ll go deep into PM2’s capabilities — from the basics of starting your first process to advanced deployment strategies using ecosystem files, clustering, and monitoring.

🌟 What is PM2 and Why Do You Need It?

PM2 is a Node.js process manager that simplifies the process of running and managing your applications in a production environment. It’s designed to manage both single-process and multi-process applications, offering features like:

  • Automatic restarts on crashes or exceptions

  • Cluster mode for multi-core utilization

  • Built-in logging and monitoring

  • Auto-start on system reboot

  • Zero-downtime reloads

  • Load balancing

  • Process scaling

  • Web-based monitoring (PM2 Plus)

It abstracts the underlying complexity of managing production apps so that you can focus on writing code, not fixing crashes at 3 A.M.

📦 Installing PM2

To get started with PM2, you need to have Node.js and npm installed.

Install PM2 globally using npm:

npm install pm2 -g

Verify the installation:

pm2 --version

You’re now ready to manage Node.js apps using PM2!

🔧 Basic Usage: Managing Your App

Let’s say you have a simple index.js file that runs a server. To start it with PM2:

pm2 start index.js

By default, PM2 assigns a process ID and a name (based on the script filename). But you can assign a custom name:

pm2 start index.js --name my-app

List all running processes:

pm2 list

To view logs in real-time:

pm2 logs

You can also manage the app lifecycle with:

pm2 restart my-app   # Restart app
pm2 stop my-app      # Stop app
pm2 delete my-app    # Delete app from PM2

🔄 Ensuring Your App Survives Server Reboots

One of the most powerful features of PM2 is its ability to auto-start apps on system reboot.

Step 1: Generate the startup script

pm2 startup

PM2 will generate and show a command that you need to execute to configure the OS-level startup task.

Step 2: Save the current state of PM2

pm2 save

Now, whenever your server reboots, PM2 will automatically start your apps.

🧠 Clustering Your App for Performance

Node.js runs in a single-threaded environment, which means it doesn’t utilize multiple CPU cores by default. PM2 makes it incredibly simple to enable clustering, allowing you to fork multiple instances of your app to run concurrently.

Use the -i flag to define the number of instances:

pm2 start index.js -i max

Here, max tells PM2 to run as many instances as the number of available CPU cores.

You can also specify a number:

pm2 start index.js -i 4

This improves performance and handles more concurrent requests.

📁 Using ecosystem.config.js for Multi-App Management

PM2 supports configuration files to manage complex deployments. This is especially useful for deploying multiple apps or setting environment variables.

Here’s an example ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: "api-server",
      script: "server.js",
      instances: "max",
      exec_mode: "cluster",
      env: {
        NODE_ENV: "development"
      },
      env_production: {
        NODE_ENV: "production"
      }
    },
    {
      name: "worker",
      script: "worker.js",
      instances: 1
    }
  ]
};

To start using the config:

pm2 start ecosystem.config.js --env production

This is also the file used when deploying via PM2’s integrated deployment system.

📜 PM2 Log Management

PM2 organizes logs by process and stores them in separate files for standard output and errors:

pm2 logs            # View all logs in real-time
pm2 logs my-app     # View logs for a specific app

You can also clear logs to avoid growing disk usage:

pm2 flush

Or reload log files without restarting the app:

pm2 reloadLogs

Log files are typically located in:

~/.pm2/logs/

📊 Monitoring Your App with pm2 monit

To keep an eye on performance and memory usage in real-time, use the built-in monitoring tool:

pm2 monit

It displays CPU and memory usage for all processes, as well as status and event loop activity. This is incredibly helpful for identifying bottlenecks or memory leaks.

🧑‍💻 Deploying Projects with PM2

PM2 supports SSH-based deployment automation through its ecosystem file. Add a deploy section like this:

module.exports = {
  apps: [...],
  deploy: {
    production: {
      user: 'ubuntu',
      host: 'your.server.ip',
      ref: 'origin/main',
      repo: 'git@github.com:your/repo.git',
      path: '/var/www/app',
      'post-deploy':
        'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

Run deployment:

pm2 deploy production

This will SSH into your server, pull the latest code, install dependencies, and restart the app.

📡 PM2 Plus: Web-Based Monitoring

PM2 also offers PM2 Plus, a SaaS-based monitoring dashboard for more advanced features:

  • Centralized logs

  • Historical CPU/memory graphs

  • Error tracking

  • Alert notifications

  • Custom metrics

To enable PM2 Plus:

pm2 link

Sign in or create an account and follow the instructions. PM2 Plus is free for small projects and paid for enterprise usage.

🧼 Cleaning and Resetting PM2

If you want to reset all apps and logs:

pm2 delete all     # Remove all managed apps
pm2 flush          # Clear all logs

Uninstall PM2 if no longer needed:

npm uninstall -g pm2

🧪 Common Use Cases

Use CasePM2 SolutionKeep app running foreverpm2 start app.jsRun multiple instances on different portsUse multiple --env or pass PORT in configRestart after crashBuilt-in crash recoveryRestart without downtimepm2 reload app.jsRun background workerAdd it in ecosystem.config.jsMonitor resource usagepm2 monit or PM2 Plus

✅ Best Practices for PM2

  • Always use ecosystem.config.js in production for reproducibility.

  • Use pm2 save after making changes to persist across restarts.

  • Regularly flush logs to save disk space.

  • Combine PM2 with a process monitoring tool like New Relic or Datadog for detailed analytics.

  • Secure your server if you’re using pm2 deploy.

🧩 Conclusion

PM2 isn’t just a process manager — it’s a powerful tool that streamlines deployment, ensures high availability, and adds observability to your Node.js apps. With a rich set of features like clustering, monitoring, and deployment support, PM2 becomes an essential part of every Node.js developer’s toolkit.

Whether you’re a solo developer working on a side project or managing large-scale production servers, PM2 makes it easy to deploy and scale your applications with confidence.

💬 Let’s Connect!

If you found this article helpful, share it with your network or leave a comment. Got questions or want to dive deeper? Feel free to reach out to me on LinkedIn or GitHub.

0
Subscribe to my newsletter

Read articles from Shubham Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Singh
Shubham Singh