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.

Related Posts

File Permissions Linux Techhyme

Understanding Common File Permissions in Linux

File permissions are a fundamental aspect of file management in Unix-based operating systems like Linux. They play a crucial role in determining who can access, modify, or…

Python Variables Store Strings Techhyme

How to Store Strings in Variables in Python

In the world of programming, Python stands out as one of the most popular and versatile languages. One of its fundamental concepts is the use of variables,…

Linux System Key Components Techhyme

7 Major Key Components That Make Up A Linux System

Linux, an open-source operating system, has become one of the most widely used platforms due to its stability, security, and flexibility. It is comprised of various components…

File Finding Commands Linux Techhyme

Top 4 File Finding Commands in Linux

In the vast world of Linux, one of the most common tasks is finding files and directories scattered throughout the file system. Fortunately, Linux provides several powerful…

Informational Commands Linux Techhyme

Top 9 Essential Informational Commands in Linux

In the world of Linux, knowledge is power, and understanding the status of your system and its processes is crucial for efficient management and troubleshooting. Thankfully, Linux…

Linux File Tree Techhyme

Exploring the Linux File Tree: A Comprehensive Guide

In the world of operating systems, Linux stands out as a powerful and versatile option, favored by system administrators and users alike. One of the distinctive features…

Leave a Reply