A Guide to PM2 Commands for Node.js Process Management

NodeJs PM2 Commands Techhyme

PM2 (Process Manager 2) is a popular and powerful process manager for Node.js applications. It simplifies the deployment and management of Node.js processes, making it easier for developers to maintain their applications in production environments. PM2 provides a comprehensive set of commands that allow you to start, stop, restart, and monitor your applications seamlessly.

In this guide, we will explore the most essential PM2 commands and their functionalities.

Getting Started with PM2

To start using PM2, you must first install it globally on your system using npm:

npm install -g pm2

Once PM2 is installed, you can access its commands from the command line.

  1. Start Commands
    • Start an Application
    • Start a Stopped Application
    • Start an Application with a Specific Configuration
    • Start an Application in Cluster Mode
  2. Management Commands
    • List All Processes
    • Save Process List
    • Restart an Application
    • Reload an Application
    • Stop an Application
    • Stop All Running Instances
    • Delete an Application
    • Delete All Instances
    • Generate Sample Configuration
  3. Monitoring Commands
    • Show Application Details
    • View Application Logs
    • View Application Environment Variables
    • Monitor All Applications

1. Start Commands

1. Start an Application:

pm2 start <file>

This command allows you to start a Node.js application using PM2. Replace `<file>` with the path to the main file of your Node.js application (e.g., `app.js`, `server.js`, etc.).

2. Start a Stopped Application:

pm2 start <app_id>

If you previously stopped an application and want to restart it, use this command. Replace `<app_id>` with the PM2 application ID (you can get this ID from the `pm2 ls` command).

3. Start an Application with a Specific Configuration:

pm2 start <app_id> ecosystem.config.js

Use this command to start an application with a specific ecosystem configuration file. Replace `<app_id>` with the PM2 application ID, and `ecosystem.config.js` with the path to your configuration file.

4. Start an Application in Cluster Mode:

pm2 start <file> -i <number_of_instances>

To run your application in cluster mode with multiple instances, use this command. Replace `<file>` with the path to your main application file, and `<number_of_instances>` with the desired number of instances.

2. Management Commands

1. List All Processes:

pm2 ls

This command provides a list of all running applications managed by PM2, along with their status and other relevant information.

2. Save Process List:

pm2 save

Use this command to save the list of processes managed by PM2 so that they can be automatically resurrected upon system reboot.

3. Restart an Application:

pm2 restart <app_id>

This command allows you to restart a specific application managed by PM2. Replace `<app_id>` with the corresponding application ID.

4. Reload an Application:

pm2 reload <app_id>

Use this command to reload an application without stopping it. Reloading helps to apply changes made to the code without downtime.

5. Stop an Application:

pm2 stop <app_id>

To stop a specific application managed by PM2, use this command and replace `<app_id>` with the PM2 application ID.

6. Stop All Running Instances:

pm2 stop all

This command stops all applications that are currently managed by PM2.

7. Delete an Application:

pm2 delete <app_id>

If you want to permanently remove an application from PM2’s management, use this command and specify the corresponding application ID.

8. Delete All Instances:

pm2 delete all

This command deletes all applications from PM2’s management.

9. Generate Sample Configuration:

pm2 ecosystem

Use this command to generate a sample ecosystem.config.js file. The ecosystem file is used to configure and manage applications.

3. Monitoring Commands

1. Show Application Details:

pm2 show <app_id>

This command provides detailed information about a specific application managed by PM2. Replace `<app_id>` with the PM2 application ID.

2. View Application Logs:

pm2 logs <app_id> --lines=<number_of_lines>

To view the recent logs of an application, use this command. Replace `<app_id>` with the PM2 application ID and `<number_of_lines>` with the desired number of log lines.

3. View Application Environment Variables:

pm2 env <app_id>

This command displays all environment variables related to a specific application.

4. Monitor All Applications:

pm2 monit

Use this command to monitor the logs, metrics, and other important information of all applications managed by PM2.

PM2 is an indispensable tool for managing Node.js applications in production environments. With its easy-to-use commands, you can efficiently handle process management, deployment, and monitoring. Mastering these commands will undoubtedly boost your productivity and give you better control over your Node.js applications. Happy coding!

Remember that PM2 evolves over time, and it’s a good practice to check for updates and the latest documentation to stay up-to-date with its features and enhancements.

You may also like:

Related Posts

Leave a Reply