Today we will look the PM2 commands list in a node on Linux, which is very useful to manage the PM2 tool.
So we will look at how to list the process, view logs, Actions like start, stop, reload, restart, delete for the PM2 Commands node in Linux.
Cheat Sheet PM2 commands list node in Linux
PM2 list commands
This will be used to list the process that is running on the server with the help of PM2 command node.
$ pm2 list
This will show all the running process by the PM2, so there are two processes that are running with the name “tastethelinux” and “tla”.
Let’s discuss the output of the PM2 node,
- id: As you can see there is an id 0 as well as 1 means that 2 applications running on the server. one with the name tastethelinux and the second one is tla.
- name: This is the name of an Application tastethelinux and tla.
- mode: This is showing the mode as a fork, Means both the server is running as a Standalone server, If you will run PM2 cluster then in the place of fork it will show as a cluster mode.
- ↺: This sign is of restart that how many time the process got restarted.
- status: The status you can see is online, Means the server is up and running.
- CPU: It will show the utilization of the application.
- Memory: It will show the consumption of the memories.
- User: Who started this process in the PM2 that is “tla”.
- Watching: Any changes in the code will automatically reload the Process, but this is disabled by default.
What if you have to describe the Process?
$ pm2 describe 1
This will give the detail information about the process ID 1 like status, uptime, the path of the logs.
PM2 logs commands
In all the Server the logs are one of the important parts and we will see that which PM2 logs commands will use to see the logs.
$ pm2 logs
By using this command “pm2 logs” we can see the current logs of the process.
So currently we have two processes running with the id 0, 1 and the process name as tla, tastethelinux.
What if you have to view the logs with the Process ID?
$ pm2 logs 1
So pm2 logs 1, the 1 is the process id with the process name tla, if we have to check the current logs for the process ID “0”
$ pm2 logs 0
What if we have to clear all the logs in PM2?
$ pm2 flush
By using the pm2 flush, it will clear all the logs of all the process that are running in the server. “pm2 logs flush”
Follow the link if you want to learn about the log Management via PM2 logrotate.
PM2 Action commands
In the Action commands of PM2 commands, the list is PM2 start command, PM2 stop all, PM2 stop process, PM2 restart, PM2 restart app, PM2 restart all.
— PM2 start command —
A. How to START the Node js Process?
$ pm2 start tla.js
So this will start the process with the name “tla” and the id “0”, what if we have to start the process with the name “TLA-PROCESS”.
$ pm2 start tla.js --name TLA-PROCESS
We have used –name option to give the name to the process.
B. How to start the process by id?
$ pm2 start 1
So, as you can see that the process id 1 has stopped, and we had started the process by using the Process ID
C. How to start all the process via PM2 commands?
$ pm2 start all
This will start all the stopped process that will be in the PM2 Manager.
— PM2 stop all —
A. How to stop all the process that are running?
$ pm2 stop all
By using pm2 stop all command, all the application was stopped in PM2
— PM2 stop process —
B. PM2 stop by id, PM2 stop by name.
Now I have to stop the application tla, so we have used the command pm2 stop ID.
$ pm2 stop 1
Suppose you have to stop pm2 by name then you have to use sudo pm2 stop APP_NAME
$ pm2 stop tastethelinux
— PM2 restart —
A. PM2 restart all.
$ pm2 restart all
To restart all the Applications in the list use pm2 restart all.
B. PM2 restart app
$ pm2 restart 1
If you want to restart the first application “tla” then use “sudo pm2 restart 1“.
$ pm2 restart tla
Also, we can restart the app by using the name in the place of Process ID.
PM2 Delete commands
So, PM2 delete commands will delete the ongoing process on the Server. we can use PM2 delete commands by Process ID, Name or can delete all Process.
A. PM2 delete process.
$ pm2 delete 0
So, the process named as tastethelinux is running with the process ID “0”, and we have deleted that process by using its ID.
$ pm2 delete tastethelinux
So, here we have used Name of the Application instead of Process ID.
B. PM2 delete all
$ pm2 delete all
So, now we have to delete all the process running by PM2 and instead of ID and name just put “all”.
PM2 Cluster Mode
PM2 clustering mode is mainly used to make the performance better and reliability for the NodeJS applications.
If you want to learn PM2 cluster mode refer to this link and get the detailed knowledge.
$ pm2 start tla.js -i 0
So, when we use -i 0 it will check the number of cores and start that number of processes.
Suppose you have 8 cores then it will start the 8 processes of the same application.
Now we have to run only 4 process of the same Application.
$ pm2 start tla.js -i 4
So, after using -i 4 will start the 4 tla running process. you can scale up and down. If want to learn how to Scale-up and scale down PM2 cluster mode refer the link.