nodejs

Nodejs application monitoring is very important in the production environment. Because There is a lot of critical part in Nodejs application like memory usage, memory leak, deployment process, etc. In this article, you will learn How to properly monitor your Nodejs application using PM2.

What is PM2

PM2 is a process manager for Nodejs application and It is simple and easy to use. PM2 is providing an easy way to manage and daemonize(runs as a background process) Nodejs applications. It will help us for reloading application in case of any exception.

pm2

PM2 supports for NodeJS based web application framework like Express, Hapi, Geddy and Sail etc.

Key features:

  • PM2 manages all process (start/stop/restart/delete/show/monit)
  • Built-in load balancer
  • Process Monitoring
  • Max memory reload without downtime
  • Keep your applications alive forever
  • Easy log management for processes
  • View performance metrics on your application
  • Development, Deployment workflow
  • Startup Scripts
  • Keymetrics monitoring on web platform

Installation:

Using node package manager(npm), you can install PM2 on the server.

npm install pm2 -g

This package should be installed as a global node.

Usage:

Start:

PM2 is having the simplest command to start, daemonize and monitor your application.

pm2 start helloworld.js You can set the name for your process using below command. pm2 start helloworld.js –name MyWorld

pm2-start

List:

Using below command, you can display all the processes with status.

pm2 list

Stop:

To stop an application, you can use below command.

pm2 stop <id|name>

//Stop all processes

pm2 stop all

Restart:

Restart the previous app launched by name

pm2 restart <id|name>

pm2 restart all

Delete:

stop and delete a process from pm2 process list

pm2 delete <id|name> pm2 delete all

Details:

To get more details about an application

pm2 show <id|name>

Monitor:

To monitor the resource usage of your application

pm2 monit pm2 monit <id|name>

pm2-monit

Kill:

To stop the PM2

pm2 kill

Logs:

Your application logs are stored into server hard disk.

pm2 logs

PM2 with Keymetrics:

https://keymetrics.io/

Keymetrics is a web platform for monitoring and managing your application using PM2. There are lot of features in keymetrics like Memory Monitoring, exception reporting, load balancer, process control and more. Just you have run your application using PM2 and Keymetrics will give more details about your application.

pm2-keymetrics

Sample Script:

pm2world.js

var http = require(“http”);

http.createServer(function (request, response) {

response.writeHead(200, {‘Content-Type’: ‘text/html’});

response.end(‘<h1>PM2 Hello World</h1>\n’);

}).listen(8084);

console.log(‘Server running at http://127.0.0.1:8084/’);

C:\pm2 start pm2world.js

[PM2] Starting C:\pm2world.js in fork_mode (1 instance)

[PM2] Done.

pm2 output