06 Mar 2018

PM2 – Advanced Node.js Process Manager

Have you worked on any Node JS application recently? However, you may not be familiar with how these applications are managed on the production server!!!

We recently had a project where we had to set up a Node JS application on Linux server. We needed a tool to solve the technical challenges of managing the Node processes and deploying the application in a very efficient manner. We choose PM2, which is a handy process management tool for running Node JS applications in production environment.

First of all, PM2 provides a simple command line interface that makes it easy to get started. This powerful Node module has tools for managing application processes, logging, and more.

In this article, We will provide an introduction to PM2 and show the basic features and commands –

What is Process Manager 2?

PM2 or Process Manager 2, is an Open Source, Node.js process manager helping Developers and DevOps manage Node.js applications in production environment.

In comparison with other process managers like Supervisord, Forever, Systemd, some key features of PM2 are automatic application load balancing, declarative application configuration, deployment system and monitoring.

Why is PM2 so popular?

Unexpected errors, exceptions, crashes will keep happening all the time. You know what’s worse? It’s not knowing that something bad happened in your Node process, and that too in a production environment. It’s a nightmare for any programmer. Now, If you are using a process manager 2, your node process will be reloaded whenever an unhandled exception occurs. So, unless you check the logs you won’t find out the issue. The solution is to use a monitoring service and have them alert you via email/SMS in case your process gets killed and restarted. Yaay!!!

Comparison with Forever and Supervisord

Process Management configuration –

PM2 has a great process management compared to Forever and Supervisord.

PM2 manages your applications states: start, stop, restart and delete processes. Hence handling node processes and occasional errors become easier and efficient. The command “pm2 list” gives an easy to read listing of all apps.

Load Balancing, Clustering & Zero second Downtime Reload –

The Cluster Mode starts instances of your app and automatically balances the load between each instance. This allows increasing overall performance depending on the number of CPUs available.

Web Interface –

PM2 provides Web Interface called Keymetrics. It can be used with an enterprise (paid) add-on service. It helps to monitor reliability and performance of application easily.

Email notifications –

There’s facility to send email based on your requirement, you can install “pm2-notify” module for the email notification. It allows sending an email along with logs.

Log management

PM2 is displaying logs of a specified process or all processes, in real time. You can avail the logs in Standard, Raw, JSON and as formatted output.

Automated deployment

PM2 provides the facility to automate the deployment process. All you need to do is configure it in a file and PM2 will pull the source from git, generate build and restart the process.

Basic Use

PM2 setup

# npm install pm2@latest -g

Firstly you need to install it globally using the “-g” flag via NPM:

# npm install pm2 -g

You can then start apps using the start command. “-n” flag for specifying app name:

# pm2 start app.js -n “appname”

You can get a list output of apps:

# pm2 list

You can restart, delete and stop using below commands:

# pm2 restart 0

# pm2 stop 0

# pm2 delete 0

You can see all the logs to get any errors:

# pm2 logs

Managing a Module

PM2 has a simple and powerful module system. Anyone can create and publish a module.

PM2 modules are pulled from the NPM repository and are published as common Javascript libraries on NPM.

Install

# pm2 install <module-name>

Restart

# pm2 restart <module-name>

Uninstall

# pm2 uninstall <module-name>

Get Module logs

# pm2 logs <module-name>  

Conclusion

PM2 is a powerful process manager specifically designed to work with Node JS application. It makes it easier to manage your application and services in the production environment and life, easier and productive for the developers.
 

PM2 compatible modules

https://github.com/keymetrics/pm2-logrotate

https://github.com/keymetrics/pm2-server-monit

https://github.com/soyuka/pm2-notify

Learn more about PM2

http://pm2.keymetrics.io/docs/usage/quick-start/