In-depth Guide to PM2: The Process Management Tool for Node.js Applications

PM2 is a process management tool specifically designed for Node.js applications, aimed at simplifying the deployment and management of applications. It offers features for starting, stopping, restarting, and monitoring applications, along with advanced features such as automatic restarts, load balancing, and log management. Here is a detailed introduction and usage guide for PM2.

Main Features

  1. Process Management: Start, stop, restart, and delete applications.
  2. Monitoring: Real-time monitoring of application status, CPU, and memory usage.
  3. Log Management: View and manage application logs.
  4. Load Balancing: Achieve load balancing through cluster mode.
  5. Automatic Restart: Automatically restart applications when they crash or encounter errors.
  6. Startup Scripts: Automatically start applications on system boot.
  7. Configuration Management: Manage multiple applications using JSON configuration files.

Installing PM2

To use PM2, you first need to install it. It can be installed via npm:

Terminal window
1
npm install -g pm2

Basic Usage

Starting an Application

To start an application, use the pm2 start command and specify the application entry file:

Terminal window
1
pm2 start app.js

Viewing Application Status

Use the pm2 list command to view all running applications and their status:

Terminal window
1
pm2 list

Stopping an Application

Use the pm2 stop command to stop a specific application:

Terminal window
1
pm2 stop app.js

Or stop it by its application ID:

Terminal window
1
pm2 stop 0

Restarting an Application

Use the pm2 restart command to restart a specific application:

Terminal window
1
pm2 restart app.js

Or restart it by its application ID:

Terminal window
1
pm2 restart 0

Deleting an Application

Use the pm2 delete command to delete a specific application:

Terminal window
1
pm2 delete app.js

Or delete it by its application ID:

Terminal window
1
pm2 delete 0

Advanced Usage

Starting Multiple Instances (Cluster Mode)

Use the -i option to start multiple instances to take advantage of multi-core CPUs:

Terminal window
1
pm2 start app.js -i max

max will start as many instances as there are CPU cores. You can also specify a specific number of instances, such as -i 4 to start 4 instances.

Managing Logs

To view logs for a specific application:

Terminal window
1
pm2 logs app.js

Or view logs for all applications:

Terminal window
1
pm2 logs

Monitoring Applications

Use the pm2 monit command to monitor the status of all applications in real-time:

Terminal window
1
pm2 monit

Using a JSON Configuration File

PM2 allows managing applications using a JSON configuration file. First, create an ecosystem.config.js file:

1
module.exports = {
2
apps: [
3
{
4
name: "app",
5
script: "./app.js",
6
instances: "max",
7
exec_mode: "cluster",
8
watch: true,
9
env: {
10
NODE_ENV: "development",
11
},
12
env_production: {
13
NODE_ENV: "production",
14
},
15
},
16
],
17
};

Then, start the applications defined in the configuration file using the pm2 start command:

Terminal window
1
pm2 start ecosystem.config.js

Setting Up Automatic Startup

PM2 can be configured to automatically start applications on system boot. First, generate the startup script:

Terminal window
1
pm2 startup

Then save the current process list:

Terminal window
1
pm2 save

Reloading Applications

To reload an application seamlessly (without downtime), use the pm2 reload command:

Terminal window
1
pm2 reload app.js

Or reload it by its application ID:

Terminal window
1
pm2 reload 0

Summary of Common Commands

  • Start an application: pm2 start <app>
  • Stop an application: pm2 stop <app>
  • Restart an application: pm2 restart <app>
  • Delete an application: pm2 delete <app>
  • View the application list: pm2 list
  • View application logs: pm2 logs <app>
  • Real-time monitoring: pm2 monit
  • Automatic startup: pm2 startup, pm2 save

With these features, PM2 provides a complete solution for managing the lifecycle of Node.js applications, simplifying the complexity of application management, and improving stability and reliability in both development and production environments.