PM2 is a production process manager for the JavaScript runtime Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without the downtime, and to facilitate common system admin tasks. PM2 is constantly assailed by more than 1800 tests.
Why PM2??
PM2 is a daemon process manager that will help you manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.
What does pm2 solve?
- pm2 can re-launch Node service after it crashes.
- pm2 can re-launch Node service after the server reboot
- pm2 can run multiple processes with multiple cores of CPU to achieve the Load Balancer like effect.
- It features kind of rolling update, 0 downtime upgrade with Graceful Reload
- Multiple services with multiple processes, significantly boost the performance.
- Schedule to restart periodically
- pm2 provides much information, including restart number, CPU usage, memory usage, process id, etc.
- pm2 allows auto-restart under specific conditions, such as ‘up-time’, ‘memory usage’, etc.
- pm2 can organize log, periodically split the log, and keep the number we specify, delete exceeded ones.
- pm2 provides a simple deployment method and supports multiple server deployments.
- pm2 can integrate with CI / CD tool, as well as CI / CD deployment.
Uses for PM2:
PM2 has a lot of uses, let’s look at a few:
- Restarting after crashes: PM2 allows us to keep processes running until the heat death of the universe, or a server failure, whichever happens first
- Monitoring and managing processes remotely: A magic-powered web portal allows you to keep an eye on remote processes and manage them
- It doesn’t just run Node apps: PM2 isn’t limited to just Node.js processes, that’s right, you can even use it to keep your Minecraft server online
- Restart-Persistence: PM2 can remember all your processes and restart them after a system restart
PM2, or Process Manager 2 is an incredibly versatile production process manager written in Node.js.
PM2 is used to start and monitor Node.js application so if the application goes down ( for example if the node index.js process dies) the process manager will restart the app immediately making it available once again.
Below is the list of all the commands what you need…!!
Install using NPM or Yarn:
$ npm install pm2@latest -g#OR$ yarn global add pm2
Update the in-memory PM2:
pm2 update
Start the process:
$ pm2 start <app.js|index.js|web.js|app_name># Or set an application name with --namepm2 start app.js --name "my-app"
Stop the process:
$ pm2 stop <app_name|namespace|id|’all’|json_conf>
Restart the process:
$ pm2 restart <app_name|namespace|id|’all’|json_conf>
Reload the process:
$ pm2 reload <app_name|namespace|id|’all’|json_conf>
Delete the process:
$ pm2 delete <app_name|namespace|id|’all’|json_conf>
List all the process:
$ pm2 [list|ls|l|status] # Display all processes status$ pm2 jlist # Print process list in raw JSON$ pm2 prettylist # Print process list in beautified JSON
Deal with logs:
View all logs in realtime:
$ pm2 logs APP-NAME #Display APP-NAME logs$ pm2 logs — json #JSON output$ pm2 logs — format #Formated output
To dig in older logs:
$ pm2 logs --lines 1000
Flush all logs:
$ pm2 flush #Flush all logs
Reload all logs:
$ pm2 reloadLogs #Reload all logs
To monitor logs, custom metrics, application information:
$ pm2 monit
Bonus!!!
Change process name:
pm2 restart id|name -n newName
Tracklogs with timestamps:
pm2 restart id|name --time
Complete information about a specific process:
pm2 describe <app_name|namespace|id|’all’|json_conf>
Process sorting:
pm2 list --sort name:desc
# Or
pm2 list --sort [name|id|pid|memory|cpu|status|uptime][:asc|desc]
Max Memory Restart:
pm2 start my-app --max-memory-restart 20M
Persistence Through Restarts:
PM2 also allows us to start our processes when our server (re)starts. Start whatever you want to have running all the time through pm2 start
and then run pm2 save
.
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in C:\Users\moose\.pm2\dump.pm2
Now, when our system restarts, PM2 will start whatever processes we had running when we ran pm2 save
.
🐊 PM2 does a lot of things, most of which are out of the scope of this brief introduction. In the future, we might cover PM2’s cluster mode, remote process management, and more. Thanks for reading!!