DEV Community

Cover image for Install PM2 (Process Manager 2)
Batkhuu
Batkhuu

Posted on

Install PM2 (Process Manager 2)

PM2 (Process Manager 2)

Process manager tool based on Node.js.
It's a tool that allows you to manage your applications (Node.js/python/binary/shell script/etc...).

Features

  • Start/stop/restart applications on demand
  • Easy to manage multiple processes
  • Run applications in the background
  • Monitor logs and resources usage
  • Restart applications on source code changes or failure
  • Startup on system reboot (service)
  • Environment based configuration
  • PM2 Plus - Advanced features (Web interface, cluster mode, etc...)

Official pages


Prerequisites

Installation

# Install PM2 through NPM:
npm install -g pm2

# Install pm2-logrotate to rotate logs for PM2:
pm2 install pm2-logrotate

# Check PM2 is running and check the version:
pm2 ping
pm2 -v
Enter fullscreen mode Exit fullscreen mode

👍 ✨

INSTALLATION COMPLETE


Usage

### Run application:

## Start node.js application in the background:
pm2 start app.js

## Start shell script app with naming "script-app":
pm2 start script.sh -n script-app

## Start the python application and watch for changes:
pm2 start python-app.py --watch

## Start binary file with arguments (--port 8080):
pm2 start binary-file -- --port 8080

## Use ecosystem configuration file:
# Generate a simple ecosystem.config.js file:
pm2 init simple
# Update the ecosystem.config.js file according to your needs:
vi ecosystem.config.js
# Start ecosystem.config.js:
pm2 start ecosystem.config.js

## Or use JSON file and start in the foreground:
pm2 start pm2-process.json --no-daemon

## Or use YAML file and specify log file path:
pm2 start pm2-process.yml --log /path/to/logs


### Check logs:
pm2 logs app-name --lines 50


### Restart application:
pm2 restart app-name


### Shows a list of applications registered into PM2:
pm2 list


### Monitor applications:
pm2 monit


### Stop and delete the application:
pm2 stop app-name
pm2 del app-name


### Generate ecosystem.config.js file:
pm2 ecosystem


## Startup application on system reboot:
pm2 startup
#--------------------------------------------------------------------------------
## Run according to shown command!
# For example:
# [PM2] You have to run this command as root. Execute the following command:
#      $ sudo su -c "env PATH=$PATH:/home/user/.nvm/versions/node/v12.20.1/bin pm2 startup <distribution> -u <user> --hp <home-path>"
#--------------------------------------------------------------------------------
pm2 save


### Help:
pm2 --help
Enter fullscreen mode Exit fullscreen mode

PM2 plus


References

Discussion (0)