Managing applications states
With PM2 you can easily start/restart/reload/stop/list applications in background.
Start
To start an application:
$ pm2 start api.js
You can also start any kind of application like bash commands, script, binaries:
$ pm2 start "npm run start"
$ pm2 start "ls -la"
$ pm2 start app.py
Start and display log stream
To start an app and check logs stream use the --attach
option:
$ pm2 start api.js --attach
When quitting via Ctrl-C, the app will still run in background.
Passing arguments
All option passed after --
will be passed as argument to the app:
$ pm2 start api.js -- arg1 arg2
Configuration File
When managing multiple application at the same time or having to specify multiple options, you can use a configuration file. Example with this ecosystem.config.js file:
module.exports = {
apps : [{
name : "limit worker",
script : "./worker.js",
args : "limit"
},{
name : "rotate worker",
script : "./worker.js",
args : "rotate"
}]
}
Then to start both apps:
$ pm2 start ecosystem.config.js
Read more about configuration file.
Restart
To restart an application:
$ pm2 restart api
To restart all applications:
$ pm2 restart all
To restart multiple apps at once:
$ pm2 restart app1 app3 app4
Updating environment variables and options
To update environment variables or PM2 options, specify the --update-env
CLI option:
$ NODE_ENV=production pm2 restart web-interface --update-env
Stop
To stop a specified application:
$ pm2 stop api
$ pm2 stop [process_id]
To stop them all:
$ pm2 stop all
To stop multiple apps at once:
$ pm2 stop app1 app3 app4
Note: this will not delete the application from PM2 application list. See next section to delete an application.
Delete
To stop and delete an application:
$ pm2 delete api
To delete them all:
$ pm2 delete all
Listing Applications
To list all running applications:
$ pm2 list
# Or
$ pm2 [list|ls|l|status]
To specify which order you want the application to be listed:
$ pm2 list --sort name:desc
# Or
$ pm2 list --sort [name|id|pid|memory|cpu|status|uptime][:asc|desc]
Terminal Dashboard
PM2 gives you a simple way to monitor the resource usage of your application. You can monitor memory and CPU easily and straight from your terminal with:
pm2 monit
Showing application metadata
To display metadata about an application:
$ pm2 show api
Reset restart count
To reset the restart counter:
$ pm2 reset all