PM2 and its usage in Node.js application ?

Today we will learn about an application management tool that we can use to manage the node applications efficiently. It's called PM2.

 

Contents

  1. What is PM2?

  2. How to install PM2?

  3. How to start an application using PM2?

  4. How to manage applications using PM2 ?

 

What is PM2?

PM2 is a daemon process manager that helps micro service developers in node manage and keep applications online. Using PM2 is straightforward, it offers a simple and intuitive CLI. it is installable via NPM.


How to install PM2?

We can install the latest version of PM2 using this NPM command

npm install pm2@latest -g

Another option is to add it in the package.json and just run npm install which will install all packages needed along with PM2

{
  "name": "Test-App",
  "version": "1.0.0",
  "dependencies": {
 "express": "^4.17.1"
  },
  "devDependencies": {
 "pm2": "^4.5.0"
  },
  "engines": {
 "node": "^12.18.0",
 "npm": "^6.14.4"
  }
}

How to start an application using PM2?

We have a very simple node application server in a file named node-app-server.js

const app = require("express")();
const port = 5000;
 
app.listen(port, () => {
  console.log(`My app listening at http://localhost:${port}`);
})

Then we need to specify the node server JS file in the PM2 start command. It will start a demon for your application

pm2 start <JS file>

For us that would be

pm2 start node-app-server.js

If the command is successful then you will see PM2 printing its name is DOS format and some more information.

[PM2] Spawning PM2 daemon with pm2_home=C:\Users\dibyo\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting C:\Users\dibyo\codeExHouse\NodeJs\node-app-server.js in fork_mode (1 instance)
[PM2] Done.

So we can see the process id and name of the application, the status, cpu, memory, user and a few more parameters.

While issuing the start command we can also specify a few more parameters. For example we can specify the app name, if it's not provided then the filename is used as app name, which you can see in the above output.

The command to specify an app name is

pm2 start <JS file> <app_name>

Example,

pm2 start node-app-server.js my-app

Here, you might have noticed the parameter watching, it indicates whether the watch flag is on. When the watch flag is on PM2 will automatically detect whether there are any changes in the application code, if there is then it will automatically restart the application and example to set watch is

pm2 start node-app-server.js my-app --watch

We can also set memory used, specify log file name, pass extra arguments for the application and few more. For details see the PM2 quick start guide.


How to manage applications using PM2 ?

To list all applications managed by PM2 we need to use this command

pm2 list

It will show the same table with parameters and their values, that it shows when we start an application.

To stop, restart, reload and delete we use the below command accordingly

pm2 restart <app_name>
pm2 reload <app_name>
pm2 stop <app_name>
pm2 delete <app_name>

We can use this command to show the logs that pm2 generates as well as the logs produced by the application

 pm2 logs

The output of my simple app after starting, stopping and again starting is as below:

C:\Users\dibyo\.pm2\pm2.log last 15 lines:
PM2        | 2021-04-16T23:39:41: PM2 log: RPC socket file      : \\.\pipe\rpc.sock
PM2        | 2021-04-16T23:39:41: PM2 log: BUS socket file      : \\.\pipe\pub.sock
PM2        | 2021-04-16T23:39:41: PM2 log: Application log path : C:\Users\dibyo\.pm2\logs
PM2        | 2021-04-16T23:39:41: PM2 log: Worker Interval      : 30000
PM2        | 2021-04-16T23:39:41: PM2 log: Process dump file    : C:\Users\dibyo\.pm2\dump.pm2
PM2        | 2021-04-16T23:39:41: PM2 log: Concurrent actions   : 2
PM2        | 2021-04-16T23:39:41: PM2 log: SIGTERM timeout      : 1600
PM2        | 2021-04-16T23:39:41: PM2 log: ==========================================================================
PM2        | 2021-04-16T23:39:41: PM2 log: App [node-app-server:0] starting in -fork mode-
PM2        | 2021-04-16T23:39:41: PM2 log: App [node-app-server:0] online
PM2        | 2021-04-16T23:58:37: PM2 log: Stopping app:node-app-server id:0
PM2        | 2021-04-16T23:58:37: PM2 log: App [node-app-server:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-04-16T23:58:38: PM2 log: pid=68892 msg=process killed
PM2        | 2021-04-17T00:03:44: PM2 log: App [node-app-server:0] starting in -fork mode-
PM2        | 2021-04-17T00:03:44: PM2 log: App [node-app-server:0] online
 
C:\Users\dibyo\.pm2\logs\node-app-server-error.log last 15 lines:
C:\Users\dibyo\.pm2\logs\node-app-server-out.log last 15 lines:
0|node-app | My app listening at http://localhost:5000
0|node-app | My app listening at http://localhost:5000

To see older logs we can specify the number of lines we would like to see

pm2 logs --lines 200

Next important feature of PM2 is it can run applications in cluster mode. Means multiple instances can run and those will be automatically load balanced by PM2. We can specify the maximum number of instances. We also can scale out and scale in while the application is running. There are many more capabilities PM2 provides. I recommend reading the PM2 documentation to know the commands and options available.






105 views0 comments

Recent Posts

See All