Header.svg

What is PM2?

PM2(Opens in a new tab/window) is advance, production process manager for Node.js applications. pm2 is daemon process manager keep our application online 24/7. PM2 can be installed via npm with following command.

How to install PM2 linux server?

Make sure node is installed in your server.

npm install pm2 -g

Global installation of npm packages may require sudo privilege. If above command does not work, add sudo before command.

sudo npm install pm2 -g

How to start Node application using PM2?

Starting Node application using PM2 is very easy.

pm2 start server.js

How to manage application state using PM2?

PM2 is process manager. It helps us to manage application state; start, stop, restart and delete processes.

# Start Process
pm2 start server.js
# OR set an application state with name --name or -n
pm2 start web.js --name "node-app-backend"
# Stop Process
pm2 stop node-app-backend# Restart Process
pm2 restart node-app-backend# Update Environment Variable
NODE_ENV=production pm2 restart node-app-backend --update-env# Delete Process
pm2 delete node-app-backend# List Processes
pm2 list
# Or
pm2 [list|ls|l|status]# Detail Info about Process with ID
pm2 show 0
# Here 0 is process id# Reset Restart Count - this will reset restart count
pm2 reset all# Sort Processes
pm2 list --sort name:desc
OR
pm2 list --sort [name|id|pid|memory|cpu|status|uptime][:asc|desc]
# By default sorting field is name and sorting order is asc# Watch and Restart app when files change
pm2 start server.js --watch# Pass extra arguments to the script
pm2 start server.js -- arg1 arg2 arg3# Delay between automatic restart of application
pm2 start server.js --restart-delay <delay in ms># Do not auto restart application
pm2 start server.js --no-autorestart# Logging
pm2 logs
pm2 logs <app-name>

Stopping a process does not delete a process. Application will move into stopped state.

Restart a process to move application state into running state.

PM2 is also used to manage script written in other language.

# Start Any Process Written in Other Languagespm2 start echo.pl --interpreter=perl

pm2 start echo.coffee
pm2 start echo.php
pm2 start echo.py
pm2 start echo.sh
pm2 start echo.rbInterpreter is set by default if following extension is used.{
".sh": "bash",
".py": "python",
".rb": "ruby",
".coffee" : "coffee",
".php": "php",
".pl" : "perl",
".js" : "node"
}

Advance Options in PM2

CLI

# MAX MEMORY RESTART# PM2 allows to restart application based on memory limit
pm2 start server.js --max-memory-restart 50M

JSON

{
"name" : "max_mem",
"script" : "server.js",
"max_memory_restart" : "50M"
}

PROGRAMMING

pm2.start({
name : "max_mem",
script : "server.js",
max_memory_restart : "50M"
}, function(error, process) {
// Process stuff here
});

UNIT

There are Kilobyte (K), Megabyte (M) and Gigabyte(G)

200M
100K
2G

Restart application on changes

cd /path/to/my/app
pm2 start env.js --watch --ignore-watch="node_modules"

How to update pm2?

# Updating PM2
npm install pm2@latest -g# Then update in memory pm2
pm2 update

Sign up

Sign up for news and updates from our agency.