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
π Related
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
π PM2 Documentation
π Tags
#PM2
#NodeJS
#ProcessManager
#Clustering
#Monitoring
#Deployment
#Scalability