In this tutorial, we will cover the topic “PM2 commands”. PM2 is a Process Manager that manages the NodeJS services.
It monitors the process and keeps the server up and running every time. With one command “npm install [email protected]” we can set up the PM2. With the help of PM2, you can do clustering of your application.
You can scale up and down with the help of the pm2-clustering command. Also, you can manage the logs for NodeJS with the help of pm2-logrotate.
PM2 has the official packages in the NPM repository. There is an official website for PM2 you can visit the site.
So we will be looking at How to use the PM2 commands, and we have divided it into five different topics.
- PM2 Actions commands.
- The Output of the PM2 list.
- PM2 log commands.
- PM2 Cluster mode commands.
- Quick view or cheat sheet for PM2 commands.
How to use PM2 Action Commands.
The PM2 Action command is like start, stop, restart and delete the process or an application.
So we will be looking at all the action commands in detail. So we have divided the PM2 actions into four sub-topics.
- How to start the process of Node via PM2 start.
- How to stop the process of Node via PM2 stop.
- Restart the process via the PM2 restart command.
- How to delete the process via PM2 delete.
1. How to start the process of Node with PM2 Start commands.
So to start the process, we should be in our source code directory. And in that, we should have the .js files, which help us to start the process in Nodejs.
$ pm2 start tla.js
So the above command has started the process with the name “tla” and by default assigns the ID “0” to the tla process.
Now let’s suppose you have to start the service or process with a particular name, then use the –name option with pm2.
$ pm2 start tla.js --name TLA-PROCESS
Suppose you have to start the service or process with a particular name, then use the –name option with pm2.
$ pm2 start 1
So in the above image, the process id 1 has stopped, and then we have used the above command to start the process. So we can start the process by using the Process ID.
Let’s suppose we have found that all the process is stopped, and we have to start all the process with one command. We have an option in pm2 to start all the processes in one go.
$ pm2 start all
2. How to stop the process of Node with PM2 stop commands.
Suppose we have to stop all the processes running on PM2, then use “stop all”.
$ pm2 stop all
So this will stop all the applications running on the system. Let’s suppose 10 processes running and we have to stop only 1 process. In that case, we can stop that process by its name or by its ID.
$ pm2 stop 1
The above command will stop the process by Process ID. So to stop the process by its name is “pm2 stop Name_Of_The_Process”.
$ pm2 stop tastethelinux
3. How to restart the process of Node with PM2 restart command.
Now there is a scenario, in which we have to restart all the processes that are running in PM2.
$ pm2 restart all
So the above command will restart all the applications running on the system. Now here also we can restart a specific process by its name or by its process ID.
$ pm2 restart 1
The above command will restart the process with Process ID 1, and suppose we have to restart the process by Name.
$ pm2 restart tla
So we have restarted the process named “tla” with the pm2 command.
4. How to delete the process of Node with PM2 delete command.
We will delete the process when it’s of no use. While deleting any process in PM2 check the process many times. If you are sure now we don’t need the process then only delete the process.
$ pm2 delete 0
So we have deleted the process with the process ID 0, and the same process we can delete using the Name of the process.
$ pm2 delete tastethelinux
So the “tastethelinux” is the process name in PM2. Now suppose we have done our work and very sure that now all the services are not required. In that case, use the command “pm2 delete all” to delete all the processes running on the system.
$ pm2 delete all
Output explained for PM2 list commands.
The PM2 list command is to list the process that is running on a node server. So we will discuss the output for the PM2 list command.
$ pm2 list
id: ID is the Process ID of the Application. When you start any application or a process it will assign the Process ID. In our case, 2 processes are running. And the Process ID is 0 and 1.
name: This is the Name of the Process or an application, that is running or stopped on a server. In our case there is 2 process is running “tastethelinux” and “tla”.
mode: The PM2 has 2 types of mode, the cluster and fork. Fork mode is running a standalone server. Cluster mode is multiple processes running for the same application.
↺: This symbol is to represent how many times the process gets restarted.
status: The status is of 3 types, online, error, and stopped. Online means the server is up and running. When the server is stopped then the status will change to stop. And when PM2 logs find any error then the status changes to error.
CPU: It will show the CPU utilization of the application.
Memory: It will show the Memory utilization for a process or an application.
User: By which user the process or an application is initiated. Who has started the process? In our case, user “tla” has start the process.
watching: By default, it is disabled. If you make any changes in code on the server, then it will restart the process automatically.
Also, you can use PM2 describe command to get the detail about the process or an application.
$ pm2 describe 1
The above command will give detailed information about the process ID 1. It will provide us with the detail like status, uptime, the path of the logs, etc…
How to check logs with PM2 log commands.
The logs are the most important for any Developer or DevOps to identify the issue. The source, the Error code, the total time to complete the request, and the response code are there in the log.
$ pm2 logs
So by using the above command, we can see the current logs for all the applications that are running on a server.
But let’s suppose you have to see the logs of Process ID 1, then use the command “pm2 logs 1”.
$ pm2 logs 1
What if you have to clear the logs for PM2 because storing logs in a server will increase the Disk space. And after some time, you find no space left on the device.
We have 3 solutions that will take care of your disk space. Use Logrotate, Use pm2-logrotate or Clear the logs by using the “pm2 flush” command.
$ pm2 flush
The above command will delete all the logs for the PM2, and the default is in the user’s home directory in the .pm2 folder.
PM2 cluster mode commands
PM2 clustering mode makes the performance better and reliability for the NodeJS applications. For detail, you can find the pm2 clustering article.
$ pm2 start tla.js -i 0
So, when we use -i 0 it will check the number of cores and start that many numbers of processes. Suppose you have 8 cores then it will start 8 processes or applications in PM2.
But what if we have to run only 4 processes in the cluster mode, then use “-i 4” as an argument.
Now we have to run only 4 process of the same Application.
$ pm2 start tla.js -i 4
You can scale up and down the cluster mode with the help of PM2 commands.
Quick View or Cheat Sheet PM2 commands.
Below is the cheat sheet for the PM2 commands used in this article.
Cheat Sheet PM2 commands list node in Linux
How to list the process in PM2? Command: # pm2 list
How to describe the process in PM2? Syntax: # pm2 describe [pid] Command: # pm2 describe 1
How to see the current logs in PM2? Command: # pm2 logs
How to see the current logs for a Particular ID in PM2? Syntax: # pm2 logs [pid] Command: # pm2 logs 1
How to START the Node js Process in PM2? Command: # pm2 start tla.js
How to START the Node js Process with Name in PM2? Command: # pm2 start tla.js --name TLA-PROCESS
How to start the process by id in PM2? Command: # pm2 start 1
How to start all the process via PM2 commands? Command: # pm2 start all
How to stop all the process that are running? Command: # pm2 stop all
How to stop the process by ID in PM2? Command: # pm2 stop 1
How to stop the process by Name in PM2? Command: # pm2 stop tla
How to restart all the process in PM2? Command: # pm2 restart all
How to restart app by ID in PM2? Command: # pm2 restart 1
How to restart app by Name in PM2? Command: # pm2 restart tla
How to delete app by Name in PM2? Command: # pm2 delete tla
How to delete app by ID in PM2? Command: # pm2 delete 0
How to delete all app in PM2? Command: # pm2 delete all
How to start the app in Cluster Mode in PM2? Command: # pm2 start tla.js -i 0
How to start 4 process in Cluster Mode in PM2? Command: # pm2 start tla.js -i 4
In this article, we have covered the Action commands like start, stop, restart and delete. Then the description for the PM2 list command is in detail. Then How to view the logs with the help of PM2. Then we cover PM2 cluster mode with the pm2 command. Last we covered the PM2 command cheat sheet. If you find any issues or feedback, then please let us know in the comment section. Thanks for reading such a long article.
One Reply to “PM2 Commands list, PM2 Commands node Linux”