In our last article where in we created our first application on nodejs, we mentioned the fact that we cannot stop the nodejs application once started if we want to service requests. While it can turn out to be pretty difficult to start and stop, if we are going to have multiple nodejs applications running.

With Process Manager we can manage & daemonize all the nodejs applications. PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks. Refer

To install PM, we will be using NPM (Node Packaged Modules) which is basically a Package Manager for Node Modules. This is installed by default with nodejs.

To install PM on our app server issue the following command

user@LT-201:~/projects/trials/nodejstrials$ sudo npm install pm2 -g
[sudo] password for user:
/usr/local/bin/pm2 -> /usr/local/lib/node_modules/pm2/bin/pm2
/usr/local/bin/rundev -> /usr/local/lib/node_modules/pm2/bin/rundev
/usr/local/bin/pm2-dev -> /usr/local/lib/node_modules/pm2/bin/pm2-dev
/usr/local/bin/pm2-docker -> /usr/local/lib/node_modules/pm2/bin/pm2-docker
`– pm2@2.4.2
+– async@1.5.2
+– blessed@0.1.81
+– chalk@1.1.3
| +– ansi-styles@2.2.1
| +– escape-string-regexp@1.0.5
| +– has-ansi@2.0.0

To start the application in the back ground,  using PM2, it also adds your application to PM2’s process list, which is shown every time you start an application:

user@LT-201:~/projects/trials/nodejstrials$ pm2 start hello.js


PM2 process manager


Getting started


Start PM2 at boot
$ pm2 startup

Daemonize Application
$ pm2 start <app>

Monitoring/APM solution


[PM2] Spawning PM2 daemon with pm2_home=/home/user/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/user/projects/trials/nodejstrials/hello.js in fork_mode (1 instance)
[PM2] Done.
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ watching │
│ hello    │ 0  │ fork │ 14796 │ online │ 0       │ 0s     │ 9%  │ 21.3 MB   │ disabled │
Use `pm2 show <id|name>` to get more details about an app

To stop an application

user@LT-201:~/projects/trials/nodejstrials$ pm2 stop hello.js
[PM2] Applying action stopProcessId on app [hello.js](ids: 0)
[PM2] [hello](0) ✓
│ App name │ id │ mode │ pid │ status  │ restart │ uptime │ cpu │ mem    │ watching │
│ hello    │ 0  │ fork │ 0   │ stopped │ 0       │ 0      │ 0%  │ 0 B    │ disabled │
Use `pm2 show <id|name>` to get more details about an app

Information about an application can be found at

user@LT-201:~/projects/trials/nodejstrials$ pm2 info hello.js

To restart application

user@LT-201:~/projects/trials/nodejstrials$ pm2 restart hello.js

To PM2 process monitor can be viewed using the following command

user@LT-201:~/projects/trials/nodejstrials$ pm2 monit


Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.

The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots. You must also specify the platform you are running on, which is ubuntu, in our case:

  • pm2 startup ubuntu

The last line of the resulting output will include a command (that must be run with superuser privileges) that you must run:

[PM2] You have to run this command as root
[PM2] Execute the following command :
[PM2] sudo su -c "env PATH=$PATH:/opt/node/bin pm2 startup ubuntu -u sammy --hp /home/sammy"

Run the command that was generated (similar to the highlighted output above) to set PM2 up to start on boot (use the command from your own output):

  • sudo su -c “env PATH=$PATH:/opt/node/bin pm2 startup ubuntu -u sammy –hp /home/sammy”
 Move to the next article to get to learn How to Setup a Reverse Proxy Server