How to use the command 'pm2' (with examples)

How to use the command 'pm2' (with examples)

PM2 is a process manager for Node.js that is used for log management, monitoring, and configuring processes. It provides a convenient way to start, stop, and restart processes, as well as monitor them in real-time. In addition, it allows users to save and resurrect processes for easier management and recovery.

Use case 1: Start a process with a name that can be used for later operations

Code:

pm2 start app.js --name application_name

Motivation:

Starting a process with a name allows us to easily reference and perform operations on that process later. It provides a way to identify and manage specific processes running within the PM2 process manager.

Explanation:

  • start app.js: This command starts the specified Node.js script or application (app.js in this case).
  • --name application_name: This argument sets a custom name (application_name in this case) for the process. It is useful for identifying the process in later PM2 operations.

Example Output:

[PM2] Process app_name launched

Use case 2: List processes

Code:

pm2 list

Motivation:

Listing processes allows us to view the current state of all the processes managed by PM2. It provides an overview of which processes are running and their status.

Explanation:

This command lists all the processes currently managed by PM2.

Example Output:

┌─────┬────────┬──────┬───┬─────┬──────────┐
│ id  │ name   │ mode │ ↺ │ status │ cpu      │
├─────┼────────┼──────┼───┼─────┼──────────┤
│ 0   │ app_name │ fork │ 0 │ online │ 0%       │
└─────┴────────┴──────┴───┴─────┴──────────┘

Use case 3: Monitor all processes

Code:

pm2 monit

Motivation:

Monitoring processes provides real-time insights into the resource usage and status of the processes managed by PM2. It allows us to identify potential issues and ensure the smooth operation of our applications.

Explanation:

This command opens an interactive monitoring interface that displays the CPU and memory usage of each process managed by PM2. It provides real-time updates and allows users to monitor the health and performance of their applications.

Example Output:

Monitoring PM2 processes...
   (To Exit, press <Ctrl>+C again or <Ctrl>+T to toggle Top View)
┌───────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬───────────┬───────┬──────────┐
│ App name  │ id │ mode │ ↺   │ status │ cpu     │ memory │ user│ watching  │ uptime│ restarts │
├───────────┼────┼──────┼─────┼────────┼─────────┼────────┼─────┼───────────┼───────┼──────────┤
│ app_name    │ 0  │ fork │ 0   │ online │ 0%      │ 10.3m  │ user│ disabled  │ 7m    │ 0   │
└───────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴───────────┴───────┴──────────┘

Use case 4: Stop a process

Code:

pm2 stop application_name

Motivation:

Stopping a process allows us to gracefully shut down a specific process managed by PM2. It can be useful when we need to stop a specific application or perform maintenance on it.

Explanation:

This command stops the process with the specified name (application_name in this case). It sends a graceful stop signal to the process, allowing it to clean up resources and terminate gracefully.

Example Output:

[PM2] Stopping app_name id:0

Use case 5: Restart a process

Code:

pm2 restart application_name

Motivation:

Restarting a process allows us to reload the application code without completely stopping and starting the process. It is useful when we want to apply code changes or refresh the application without any downtime.

Explanation:

This command restarts the process with the specified name (application_name in this case). It stops the process and then starts it again, effectively refreshing the application code and maintaining the process state.

Example Output:

[PM2] Restarting app_name id:0

Use case 6: Dump all processes for resurrecting them later

Code:

pm2 save

Motivation:

Dumping processes allows us to save the configuration information of all the managed processes. It provides a way to back up the current state of the processes, including their startup parameters and environment variables, so that they can be resurrected later.

Explanation:

This command saves the current state of all the processes managed by PM2. It creates a JSON file (dump.pm2 by default) that contains the configuration information of each process, allowing them to be resurrected later.

Example Output:

[PM2] Saving current PM2 processes...
[PM2] Successfully saved in /path/to/dump.pm2

Use case 7: Resurrect previously dumped processes

Code:

pm2 resurrect

Motivation:

Resurrecting previously dumped processes allows us to restore the state of the processes as it was when they were dumped. It is useful for recovering the state of the processes after server restarts or failures.

Explanation:

This command loads and starts all the processes from a previously saved PM2 dump file. It restores the configuration and state of the processes, allowing them to continue running as they were when they were saved.

Example Output:

[PM2] Loaded 5 processes from /path/to/dump.pm2
[PM2] Resurrecting app_name id:0
[PM2] Resurrecting another_app id:1
...

Conclusion:

The pm2 command provides a powerful and convenient way to manage Node.js processes. With its various use cases, it allows users to start, stop, restart, monitor, and save processes, making it easier to manage and maintain Node.js applications. Whether it’s starting processes with custom names, monitoring resource usage, or saving and resurrecting processes, PM2 is a valuable tool for Node.js process management.

Related Posts

How to use the command wormhole (with examples)

How to use the command wormhole (with examples)

Wormhole is a command-line tool that allows users to securely send files or text between two computers.

Read More
How to use the command 'glab release' (with examples)

How to use the command 'glab release' (with examples)

The ‘glab release’ command is a powerful tool for managing releases in a GitLab repository.

Read More
How to use the command 'interdiff' (with examples)

How to use the command 'interdiff' (with examples)

The ‘interdiff’ command is a tool that allows users to compare differences between two diff files.

Read More