PM2
Link)
Install (sudo npm install pm2@latest -g
Commands
Start Application
pm2 start hello.js
output
App name | id | mode | pid | status | restart | uptime | memory | watching
hello | 0 | fork | 30099 | online | 0 | 0s | 14.227 MB | disabled
Stop an application (App name or id)
pm2 stop hello
Restart an application (App name or id)
pm2 restart hello
The list of applications currently managed by PM2
pm2 list
More information about a specific application (App name or id)
pm2 info hello
Monitor application status, CPU, and memory usage
pm2 monit
Check report (say error..etc)
pm2 report
Application on system reboot
(Init Systems Supported: systemd, upstart, launchd, rc.d)
sudo pm2 startup systemd
Output:
[PM2] Generating system init script in /etc/systemd/system/pm2.service
[PM2] Making script booting at startup�
[PM2] -systemd- Using the command:
su root -c "pm2 dump && pm2 kill"
&& su root -c
"systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"
[PM2] Dumping processes
[PM2] Stopping PM2�
[PM2] All processes have been stopped and deleted
[PM2] PM2 stopped
[PM2] Done.
Freeze a process list on rebbot via
pm2 saveRemove init script
sudo pm2 unstartup systemd
Using config file instead commands
create a new configuration file
pm2 ecosystem
This will generate and ecosystem.config.js file:
module.exports = {
apps : [{
name: 'API',
script: 'app.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}]
};
To start/restart/stop/delete this file via CLI
pm2 [start|restart|stop|delete] ecosystem.config.js
Other CLI methods
# Start all applications
pm2 start ecosystem.config.js
# Stop all
pm2 stop ecosystem.config.js
# Restart all
pm2 start ecosystem.config.js
## Or
pm2 restart ecosystem.config.js
# Reload all
pm2 reload ecosystem.config.js
# Delete all
pm2 delete ecosystem.config.js
Act on a specific process
pm2 start ecosystem.config.js --only api-app
pm2 restart ecosystem.config.js --only api-app
pm2 reload ecosystem.config.js --only api-app
pm2 delete ecosystem.config.js --only api-app
Switching environments
# Inject what is declared in env_production
pm2 start process.json --env production
# Inject what is declared in env_staging
pm2 restart process.json --env staging