basic about node js pm2 process management
basic about node js pm2 process management

Hi Guys, Welcome to Proto Coders Point. In this article will learn about pm2 and learn how to install pm2 in your operating system.

What is PM2 – Why we use PM2

PM2 stands for process manager, so as the name itself says PM2 is used to manage your nodejs processing script, we use pm2 to keep our nodejs script application always running even after terminal is been closed.

Basically when we run node script in terminal using node app.js and close the terminal, then the process is no longer alive therefore to keep app process alive we start the scripts by using process manager 2.

$ pm2 start app.js


Required

NodeJS & npm should be installed in your OS. check if node & npm are installed or not.

node --version

npm --version

check node & npm version

Install pm2 ubuntu / windows / macOS

To install process management(pm2), make sure node & npm is installed.

Now let install pm2 on ubuntu, not only on ubuntu, below installation steps works on all OS. run below cmds.

npm install pm2 -g

Here -g means install it globally.


How to run script using pm2

By using pm2 we can start any scripting language process (nodejs, python, ruby etc) as shown below:-

pm2 start <app-name>

eg: pm2 start app.js


Manage Application process in pm2

As we start app process you can manage & view them easily using pm2:-

get all pm2 list – running & stopped process

$ pm2 list

pm2 get list of running apps

process apps can easily managed as below

$ pm2 start    < app_name | namespace | id | 'all' >
$ pm2 stop     < app_name | namespace | id | 'all' >
$ pm2 restart  < app_name | namespace | id | 'all' >
$ pm2 delete   < app_name | namespace | id | 'all' >

monit pm2

pm2 monitor logs shows all the running script process logs to get application information:-

$ pm2 monit

monit pm2

pm2 log management

To view all running processes logs then simple type below cmd

$ pm2 logs

But this will show you all the process logs at once in real time.

Watch logs on specific script app

Then if you want to watch logs of a specific script type:-

$ pm2 logs < appname >

This will only show logs of specified process.

clear log file

To erase/clear all the log files data at once, then simple run flush as below

$ pm2 flush