Production Process Manager (PM2) for Node.JS
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
PM2 is an open-source, advances, and efficient production process manager for applications that are built in Node.JS. PM2 comes with the built-in load balancer. It allows the monitoring of the application. It allows the user to reload the application without service interruption. It supports auto restart with efficient management of applications, services, processes and the facilitation of system admin tasks.
Features
- It allows the application to be executed continuously without any interruption.
- It allows auto-shutdown.
- It supports auto-restart.
- It supports force start or stop of the applications.
- It makes the applications to run as a service.
- It makes the applications secure by not running the service as root.
- It maintains the log of each and every exception.
Installation
It can be installed on the system by the following command -
npm install -g pm2
Implementation
After installing, create a file and name it app.js. In the file, write the following code for starting the server -
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('OpenGenus');
});
server.listen(3000, (err) => {
if (err) { console.log(err); }
else{ console.log(`Server running`); }
});
To start the service, simply execute the following command -
pm2 start app.js
The output for the following command will be -
The output above is the number of processes that are displayed in the form of table making it much easier and efficient to monitor the various aspects.
The various terms in the table displayed in the image are -
- id - A unique id is given to each and every application.
- name - It displays the name of the application. By default, the application is given the name same as your main file in which all the codes are written.
- namespace - It is the class of elements i.e address, file locations, etc. By default, it has been given the default value.
- version - It represents the version you are using.
- mode - It tells us that in which mode a process is running. Fork is the default value which tells us that the process has created a copy of itself.
- pid - It represents process id
- uptime - It represents for how much duration the application has been started and working.
- restart - It tells that how many times the process has been restarted.
- status - It represents the status of the process.
- cpu - It represents the amount of CPU is being utilized.
- mem - It tells how much memory is being consumed.
- user - It tells the account in which the process is set up.
- watching - By default, it is disabled. You can watch and monitor the process by just writing:
pm2 start app.js --watch
For forcing the service to restart, write the following command -
pm2 start app.js -f
The output for the above command will be -
Now, specify an app name to your service, for example, I will give the app name as opengenus. Therefore, write the following command -
pm2 start app.js --name opengenus
The output for the above command will be -
Now, to restart the service, write the following command -
pm2 restart opengenus
For reloading the service, write the following command -
pm2 reload opengenus
For stopping a service, write the following command -
pm2 stop opengenus
For deleting a particular service, write the following command -
pm2 delete opengenus
To list the status of the services, write the following command -
pm2 status
The output for above command will be -
To display logs, write the following command -
pm2 logs
Clustering of an application
The clustering of an application greatly increases the performance and reliability of the application. It allows the NodeJs applications to be scaled accross all the CPUs available, without any modification of the code. Clustering of an application depends on the number of CPUs available. For enabling the cluster mode, write the following command:
pm2 start app.js -i max
Here, -i enables the cluster mode, and max allows PM2 to auto-detect the number of CPUs available and run as many processess as possible.
The output for the above code will be -
Now, to watch and restart the service when any change is made in the file, write the following command -
pm2 start app.js --watch
For running an application on a specific port number, write the following command -
pm2 start app.js -- --port 3000
Here, I am using the port number as 3000.
For monitoring application information, write the following command -
pm2 monit
The output for the above command will be -
Here, in the Process list section all the process that are running will be shown. In app logs the error logs(if any) will be displayed. Custom Metrics is used to monitor the in-code values in realtime. Metadata is used for displaying the data for other data. It represents data about data.
For enabling web dashboard, write the following command -
pm2 plus
It will first authenticate the user and then open the web based dashboard. It will show the current diagnostic system and all the information related to the processes and the system in which the services are being executed.
The output for the above command will be -
Conclusion
In this article at OpenGenus, we learned about the PM2 module for NodeJs. We learned the features of this module and how this module can be useful in increasing the efficiency of the application. We also implemented its various commands.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.