PM2 - Process Manager For Node.js

⚑ PM2

[!info]
PM2 is a powerful process manager for Node.js applications that helps manage and monitor processes in production environments. It ensures that applications remain alive, can restart automatically if they crash, and provides insightful metrics on performance.

🌐 Project Homepage: PM2 Homepage


πŸ” Overview

[!info]
PM2 is a Node.js process manager designed to keep applications running smoothly, even in high-demand environments. It provides several features such as automatic restarts, clustering, monitoring, and more.

Why Use PM2?

  • Automatic Restarts: Keeps your applications running by automatically restarting crashed processes.

  • Load Balancing: Supports clustering to distribute traffic across multiple instances.

  • Log Management: Aggregates and manages logs efficiently.

  • Monitoring & Metrics: Provides a built-in dashboard for real-time monitoring.

  • Process Scaling: Easily scales applications across multiple CPU cores.

  • Deployment Workflow: Simplifies application deployment with a built-in process for handling updates.


πŸ› οΈ Features

[!tip]
PM2 offers an array of powerful features for Node.js applications:

  • πŸ”„ Automatic Restart: Keeps apps alive by auto-restarting them on crashes.

  • πŸ–₯️ Clustering: Distributes traffic across multiple app instances.

  • πŸ“Š Real-time Monitoring: Dashboard for tracking application performance.

  • πŸ” Zero-Downtime Deployments: Allows deployment without interrupting the service.

  • βš™οΈ Custom Configuration: Define apps with an ecosystem file.


πŸƒ Getting Started

πŸ§‘β€πŸ’» Install PM2

To install PM2 globally, run:

npm install -g pm2

To verify installation:

npm list -g pm2

πŸ”§ Basic Usage

Start an Application

pm2 start app.js

This starts the app.js file as a PM2-managed process.

List Running Processes

pm2 list

Shows a table with information about all managed processes.

Restart a Process

pm2 restart app

Restarts the process named app.

Stop and Delete a Process

pm2 stop app
pm2 delete app

Stops and removes the process from PM2.

Auto Restart on System Boot

[!tip]
To ensure PM2 restarts applications when the system reboots, run:

pm2 startup

Then follow the instructions to enable the startup script.

Logs

To view logs in real-time:

pm2 logs

To filter logs for a specific process:

pm2 logs app

Process Scaling

To start multiple instances of an application:

pm2 start app.js -i max

To specify the number of instances:

pm2 start app.js -i 4

Monitoring

For real-time monitoring:

pm2 monit

  • Node.js β€” Platform for building scalable network applications, like PM2.

  • Docker β€” Containerization platform used with PM2 for scalable deployments.

  • Nginx β€” Web server and reverse proxy that can work alongside PM2 for load balancing.


🌍 Explore More


πŸ“š Tags

  • #PM2

  • #NodeJS

  • #ProcessManager

  • #Clustering

  • #Monitoring

  • #Deployment

  • #Scalability