Skip to main content

PM2

PM2 is a daemon process manager that will help you manage and keep your application online.

Install

npm install pm2@latest -g

Update

pm2 save
npm install pm2 -g
pm2 update

# after updating node version
pm2 unstartup && pm2 && pm2 save

# Got
# Error [TypeError]: Class extends value undefined is not a constructor or null
npm install -g [email protected]
pm2 delete $ID
pm2 start ecosystem.config.js --env production

General

pm2 ls # Display all processes status

# Display all information about a specific process
pm2 show 'APP_NAME_1'
pm2 describe 0

pm2 monit # Monitor all processes

pm2 logs
pm2 logs APP_NAME_1
pm2 flush # Empty all log files

# Supporting information
pm2 report

# For express generator
pm2 start bin/www

pm2 delete 0 # Will remove process from pm2 list
pm2 delete all # Will remove all processes from pm2 list

pm2 stop all # Stop all processes
pm2 stop 0 # Stop specific process id
pm2 restart 0 # Restart specific process id

# Generating a Startup Script
pm2 startup
pm2 startup -u $USER
# Saving the app list to be restored at reboot
pm2 save

## pm2-logrotate
pm2 install pm2-logrotate
# Watch config
pm2 conf pm2-logrotate

pm2 updatePM2 # Update in memory pm2
pm2 ping # Ensure pm2 daemon has been launched

pm2 resurrect # restore after upgrade

Configuration File

  • Must ends with .config.js.
  • Add to git repository.
# generate a sample ecosystem.config.js
pm2 init simple

vi ecosystem.config.js

# Start all applications
pm2 start ecosystem.config.js
pm2 start ecosystem.config.js -u $USER

# Stop all
pm2 stop ecosystem.config.js

# Restart all
pm2 restart ecosystem.config.js

# Reload all
pm2 reload ecosystem.config.js

# Delete all
pm2 delete ecosystem.config.js

# --only option works for start/restart/stop/delete as well
pm2 start ecosystem.config.js --only "APP_NAME_1,APP_NAME_2"

Deployment System

  • Deploy root path: /deploy:production:path/source
  • Using Git Bash at Windows OS.
# Provision remote server, tree name in `deploy`
pm2 deploy ecosystem.config.js production setup

# Deploy application
pm2 deploy ecosystem.config.js production

# if git report an error that there are local changes but still want to push what is on the remote GIT, you can use the --force option to force deployment.
pm2 deploy ecosystem.config.js production --force

pm2 start ecosystem.config.js --only hawkbit-ui

# Rollback to previous deployment
# revert to [n]th last deployment or 1
pm2 deploy ecosystem.config.js production revert [n]

# deploy to [ref], the "ref" setting, or latest tag
# deploy: {
# production:{
# ref: 'origin/master',
# ref: 'tags/TAG1',
# ref: '79a9a955e5957ec2afb3806290617de327a5e851',
pm2 deploy ecosystem.config.js production [ref]

# Execute a command on each server
pm2 deploy production exec "pm2 reload all"

App Example:

name: 'app1',
script: './node_modules/next/dist/bin/next',
exec_mode: 'fork',
watch: [
".",
],
env_production: {
NODE_ENV: 'production',
NODE_PORT: '16666'
}

Deployment Lifecycle

"pre-setup" : "echo 'commands or local script path to be run on the host before the setup process starts'",
"post-setup": "echo 'commands or a script path to be run on the host after cloning the repo'",
"pre-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"pre-deploy-local" : "echo 'This is a local executed command'"

Multi Host Deployment

"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],

Startup for a non-root user

# Replace 'ubuntu' with the appropriate distribution from this list:
# ubuntu|centos|redhat|gentoo|systemd|darwin|amazon
sudo su -c "env PATH=$PATH:$(dirname $(nvm which node)) pm2 startup ubuntu -u $USER --hp $HOME"
pm2 save

pm2-logrotate

Best practice:

  • Don't use error_file, out_file in the PM2 ecosystem file.
pm2 conf pm2-logrotate

Trouble Shooting

connect EPERM //./pipe/rpc.sock

Use administrator

Windows console error: spawn sh ENOENT

Using GitBash

No pm2 command

whereis node
sudo ln -s /home/dev/.nvm/versions/node/v16.13.0/bin/node /usr/bin/node
whereis pm2
sudo ln -s /home/dev/.nvm/versions/node/v16.13.0/bin/pm2 /usr/bin/pm2

Limitation

  • Can not display version of spring boot app.
  • Can not scan the .env file outside node project.