Learn NodeJS App Management And Getting Started With PM2 On RHEL/CentOS 6/7

In our previous article, we learned about setting up NodeJS with Nginx in front so that we can access our App with the domain. Now the problem is, if the somehow server or the application goes down then we don’t have any management scenario to get our running node app live again so PM2 came in the picture.

PM2 is a production process manager for Node.js applications with a built-in load balancer and demonize applications (run them as a service). It allows you to keep applications alive forever and reload them without downtime if required and also it facilitate common system admin tasks.

Just in case, if you did miss Previous tutorials, You can read here:

In this Tutorial, we will learn about the PM2 NodeJS app management and simple commands of PM2 to manage our running mode.


Host OS: CentOS/RHEL 6/7
Host IP:
RAM: 4GB Memory
Network Port: 1GB/s


A NodeJS Server Setup and at least one NodeJS App

Let’s Start

Install Or Update PM2 And PM2 Setup

Installation On RHEL/CentOS 6/7

Now we will install PM2an easy way to manage and to install this We will use Node Packaged Modules (NPM), so to install PM2 use this command.

[root@localhost ~]# npm install pm2 -g

As we have successfully install PM2 now we will learn how to use this. PM2 is simple and easy to use. I will cover a few basic uses of PM2.

Update PM2 On RHEL/CentOS 6/7

If you have a running PM2 then you update it for better working optimized working. To update your PM2 run the following command

[root@localhost ~]# npm install pm2@latest –g

After installing you need to update your pm2 package. run the below command which will Save the process list, exit old PM2 & restore all processes again

[root@localhost ~]# pm2 update

Setting Up PM2 Service

After Succesful installation of PM2, we need to do some basic tasks to setup PM2 service on our current server. so let’s follow given below command.

Register Your PM2

To monitor your applications and you want to register your PM2 for Keymetrics monitoring
Dashboard then you can type in the given below command and complete the registration process.

[root@localhost ~]# pm2 register

Add PM2 Service on start-up

Well, start-up command will generate and configures a script to launch PM2 and its managed processes on server boots. You may change your OS like Ubuntu centos etc. In our case we will use centos:

[root@localhost ~]# pm2 startup centos

Well output will you list of command to manage this pm2 service on CentOS 7 ( You will see a different list on CentOS 6)

systemctl enable pm2-root
systemctl start pm2-root
systemctl daemon-reload
systemctl status pm2-root

To save/freeze a process list on reboot

[root@localhost ~]# pm2 save

Note: you may remove pm2 anytime from startup using pm2 unstartup

Learn NodeJS APP Management With PM2

1. Process Management

In this section, we will learn some basic task of managing our app and its processes. Also, we will learn to check, delete add and learns some other management command for our NodeJs App

A. Start A NodeJS App As Daemon(Service)

Our app test.js can demonize with the help of pm2 and test.js will get run in the background. To do the same use the following command.

[root@localhost ~]# pm2 start /usr/share/nginx/html/test.js

Start A NodeJS App As Daemon(Service)

The above output shows that your application has been added to PM2’s process list, which is prompt every time you start an application:

This put has following information which PM2 automatically assigns:
App name: A Name without the .js extension of file
PM2 id: 0 In Current case
PID: 26105 of the process
Status: online
Memory Usage: 13.0 MB

PM2 will restart the application if somehow application crashes or killed

Note: Add PM2 launch on system startup (boot or reboot) to keep your service running even after the reboot. 

B. Stop An Application

To stop any running application, you need PM2 App name or id. According to our case, we will run below command as example

Syntax: pm2 stop <app_name|id|’all’|json_conf>

[root@localhost ~]# pm2 stop test

Stop A NodeJS App

C. Restart an application

To restart any running application, you need PM2 App name or id. According to our case, we will run below command as example

Syntax: pm2 restart <app_name|id >

We may also use variables declared in our json_conf pass it as an argument, and optionally your custom env name from your json_conf if any:

Syntax: pm2 restart <json_conf> [–env <env_name>]

[root@localhost ~]# pm2 restart test

Restart A NodeJS App

D. List Managed applications

you can check out the list and see how many apps have been managed by pm2 node manager by running below command:

[root@localhost ~]# pm2 list

NodeJs App List

E. Get Description of App

To have more details on a specific process of an app:

Syntax: pm2 describe <id|app_name>

[root@localhost ~]# pm2 describe test

NodeJs App Description

F. Delete an Application

To delete any running application, you need PM2 App name or id. According to our case, we will run below command as example

Syntax: pm2 delete <app_name|id|’all’|json_conf>

[root@localhost ~]# pm2 delete test

Delete NodeJs App

G. APP info

To get information about any app managed by PM2 you need PM2 App name or id. According to our case, we will run below command as example

Syntax: pm2 info <PM2 App name | id>

[root@localhost ~]# pm2 info test

NodeJs App Info

2. CPU / Memory Monitoring

To monitor CPU and Memory usage of a running we may use below command

[root@localhost ~]# pm2 monit

here you can see the list of all Node.js applications(multiple) running, and managed by PM2 with logs and

NodeJs App Monitoring

3. PM2 And App Logging

Displaying logs of a specified process or all processes, in real time. Standard, Raw, JSON and formatted output are available.

Syntax: pm2 logs [‘all’|app_name|app_id] [–json] [–format] [–raw]

[root@localhost ~]# pm2 logs test

NodeJs App Logs

Other Examples For Log Management:

[root@localhost ~]# pm2 logs APP-NAME # Display APP-NAME logs
[root@localhost ~]# pm2 logs --json # JSON output
[root@localhost ~]# pm2 logs --format # Formatted output
[root@localhost ~]# pm2 flush # Flush all logs
[root@localhost ~]# pm2 reloadLogs # Reload all logs

4. Load Balancing And Quick Reload

When an application is started with the -i <instance number> option, the Cluster Mode is enabled.
The Cluster Mode starts <instance number> instances of your app and automatically load balance HTTP/TCP/UDP between each instance. This allows increasing overall performance depending on the number of CPUs available.

Main commands:

[root@localhost ~]# pm2 start app.js -i max # Enable load-balancer and start 'max' instances (cpu nb)
[root@localhost ~]# pm2 reload all # Zero second downtime reload
[root@localhost ~]# pm2 scale <app_name> <instance_number> # Increase / Decrease process number

5. Addon Modules System

PM2 embeds a simple and powerful module system. Installing a module is straightforward:

Syntex: pm2 install <module name>

Here are some PM2 compatible modules (standalone Node.js applications managed by PM2):

pm2-logrotate auto rotate logs of PM2 and applications managed
pm2-webshell expose a fully capable terminal in browsers
pm2-server-monit monitor your server health

Read More In Related Article:

  1. Setup NodeJS With Nginx For Multiple App And Multiple Domain – CentOS/RHEL 6/7
  2. Learn NodeJS App Management And Getting Started With PM2 On RHEL/CentOS 6/7