Nginx Java Script React JS Node JS Angular JS Mongo DB Nginx AWS JAVA Python Type Script

NGINX Index

PM2 Commands

Before this page, we hope you have read about PM2 and its advantages. PM2 provides a range of commands through its command-line interface (CLI) for managing Node.js applications. Here are some commonly used PM2 commands:

`npm install -g pm2`: To get started with PM2, you typically install it globally using npm:

bash

npm install -g pm2

`pm2 start app`: This code for Start a Node.js application. In this command replace 'app' to your application name.Application name means the entry point of your Node.js application, typically a JavaScript file. For example, `app.js` or `server.js`.

bash

pm2 start app

`pm2 start app.js`: This code for start a specific Node.js script. In this command `pm2 start app.js` Please replace 'app.js' to your script file.

bash

pm2 start app.js

`pm2 start app -i instances`: This command is for Cluster mode. Cluster mode is particularly useful for applications that can take advantage of parallel processing and benefit from load balancing across multiple instances.

bash

pm2 start app -i instances

In this command replace `app` with the actual entry point of your Node.js application (e.g., app.js or server.js), and replace `instances` with the desired number of instances.The number of instances depends on factors such as the available CPU cores and the requirements of your application. Here we give an example:

bash

pm2 start app.js -i 4

Keep in mind that not all applications need to run in cluster mode, and the optimal number of instances may vary based on the nature of your application and the resources available on your server.

`pm2 stop app_name_or_ID`: This command is used to stop all running applications managed by PM2. When you run this command, PM2 will terminate the processes it is currently managing.If you want to stop a specific application or process, you can provide the process name or process ID as an argument.For process name:

bash

pm2 stop app

For process ID:

bash

pm2 stop 0

`pm2 restart`: This command is used to restart all running applications managed by PM2. When you run this command, PM2 will stop all the processes it is managing and then restart them. This can be useful when you've made changes to your application or its configuration.

bash

pm2 restart

`pm2 restart app_name_or_ID`:This command is used to restart specific running application .This can be useful when you've made changes to your application or its configuration,and you can restart a specific application without affecting other running processes managed by PM2.

bash

pm2 restart app

This example for Restart a specific application by process ID. Replace `0`with the actual process ID:

bash

pm2 restart 0

`pm2 reload`: When you run `pm2 reload`, PM2 will try to reload your application gracefully, allowing existing connections to finish before the new code is applied. This can help avoid disruptions to your users during the deployment process. It's a useful command for achieving zero-downtime deployments.

bash

pm2 reload

`pm2 list`: This command is used to display a list of all the processes managed by PM2. When you run this command, PM2 will provide information about each running process, including details such as process name, process ID (PID), status, and other relevant information.

bash

pm2 list

`pm2 monit`: This command is used to launch an interactive monitoring dashboard provided by PM2. When you run this command, PM2 will open a web-based user interface in your default web browser, allowing you to monitor the health and performance of your running Node.js processes

bash

pm2 monit

`pm2 logs`: This command is used to display the combined logs of all processes managed by PM2. When you run this command, PM2 will aggregate and display the logs generated by each running process. This is particularly useful for monitoring and troubleshooting issues in your Node.js applications.

bash

pm2 logs

`pm2 logs app_name_or_ID`: This command is used to display logs for a specific process applications.Replace `app_name_or_ID` with the name or process ID of the application you want to get logs.

bash

pm2 logs app_name_or_ID

`pm2 flush`: This command is used to clear (or "flush") the logs for all processes managed by PM2. When you run this command, it will remove the log entries that have been accumulated in the internal log buffer. This command can be useful when you want to clear the logs without stopping or restarting the processes.

bash

pm2 flush

`pm2 delete app_name_or_id`:
This command is used to to remove a specific application from the PM2 process list. When you run this command, PM2 stops and deletes the specified application, and it will no longer be managed by PM2. Replace `app_name_or_id` with the name or process ID of the application you want to delete

bash

pm2 delete app

`pm2 delete all`: If you want to stop and delete all applications managed by PM2, you can use the following command:

bash

pm2 delete all

`pm2 scale app_name_or_id instances`: This command is used to scale a specific application by adjusting the number of instances it runs. Scaling can be useful for distributing the load and improving the performance and reliability of your application.Replace `app_name_or_id` with the name or process ID of the application you want to scale, and `instances` with the desired number of instances.

bash

pm2 scale app 4

`pm2 show app_name_or_id`: This will display detailed information about the application, including the current number of instances and other relevant details.

bash

pm2 show app

`npm install -g pm2@latest `: When you run this command, npm checks the npm registry for the latest version of the PM2 package and installs it globally on your system. This ensures that you have the most recent version of PM2 and can benefit from any new features or bug fixes.

bash

npm install -g pm2@latest

`pm2 save`: This command is particularly useful when you want to persist your PM2 process configuration, making it easier to manage and reproduce your deployment settings. The generated `ecosystem.config.js` file can be version-controlled and shared across different environments.

bash

pm2 save

`pm2 resurrect`: The `pm2 resurrect` command in PM2 is used to restart previously saved processes based on the information stored in the ecosystem file, which is usually named `ecosystem.config.js`. This command is useful for restoring your applications to their previous state after a server restart.

bash

pm2 resurrect

`pm2 info`: This command in PM2 is used to display detailed information about the system, including the Node.js version, PM2 version, and other relevant details. It provides a comprehensive overview of the PM2 environment.

bash

pm2 info

`pm2 startup`: The pm2 startup command in PM2 is used to generate and configure a startup script that ensures PM2 starts automatically when your system boots. This is particularly useful for setting up PM2 to run as a service, making it persistent across reboots.

bash

pm2 startup

If you want to specify the init system manually, you can use the --hp flag followed by the path to the home directory of the target user. For example:

bash

pm2 startup systemd --hp /home/your_username

Keep in mind that the exact steps and commands generated by `pm2 startup` may vary based on your operating system and init system (e.g., systemd, init.d). Always follow the instructions provided by `pm2 startup` to ensure proper configuration for your specific environment. Additionally, administrative privileges might be required to execute the generated command, so use `sudo` or run your command prompt as an administrator as needed.

`pm2 kill: The `pm2 kill` command is used to kill the PM2 daemon along with all the processes it is managing. This command forcefully terminates all PM2-managed processes and stops the PM2 daemon itself.

bash

pm2 kill

`pm2 --help`: The command is used to display the help information and documentation for the PM2 process manager. When you run `pm2 --help` in the terminal, it provides a summary of available commands, options, and their descriptions. This is helpful for users who want to learn more about the functionality and usage of PM2.

bash

pm2 --help

Overall, PM2 is a powerful tool for managing Node.js processes in production, providing features for deployment, monitoring, scaling, and ensuring the reliability of your applications.Next page we will covered PM2 commands.