PM2 API
PM2 can be used programmatically, allowing to manage processes straight from the code.
Quickstart
Note: To release connection to PM2 and make your application auto exit, make sure to disconnect from pm2 with pm2.disconnect()
First add PM2 as a dependency:
npm install pm2 --save
Then create a script called app.js and pm2-control.js containing this:
const pm2 = require('pm2')
pm2.connect(function(err) {
if (err) {
console.error(err)
process.exit(2)
}
pm2.start({
script : 'api.js',
name : 'api'
}, function(err, apps) {
if (err) {
console.error(err)
return pm2.disconnect()
}
pm2.list((err, list) => {
console.log(err, list)
pm2.restart('api', (err, proc) => {
// Disconnects from PM2
pm2.disconnect()
})
})
})
})
- This will spawn or connect to local PM2
- Then start app.js with name api
- Display all applications managed with PM2
- Then restart the app with name api
- And disconnect from PM2
API Methods
pm2.connect([no_daemon_mode], fn)
Connect to local PM2 or spawn a new PM2 instance.
| Param | Type | Default | Description |
|---|---|---|---|
| [no_daemon_mode] | boolean | false | if true, it will run an independent PM2 that will auto exit at end |
| fn | function | Callback |
- no_daemon_mode: If true is passed for the first argument, pm2 will not be run as a daemon and will die when the related script exits. By default, pm2 stays alive after your script exits. If pm2 is already running, your script will link to the existing daemon but will die once your process exits.
pm2.disconnect([fn])
Disconnect from local PM2 (alias pm2.close([fn])). Required to let your script exit; the daemon and its apps keep running.
pm2.start(process, [options], fn)
Start a process
| Param | Type | Description |
|---|---|---|
| process | string/object | script path, ecosystem file path or object via options |
| [options] | object | options, when process is a script path |
| fn | function | Callback(err, apps) |
Options use the ecosystem file names, in snake_case (camelCase aliases such as maxMemoryRestart are also accepted). Most common ones (more on signals and graceful shutdown):
name- An arbitrary name that can be used to interact with (e.g. restart) the process later in other commands. Defaults to the script name without its extension (eg"testScript"for"testScript.js").script- The path of the script to run.args- A string or array of strings composed of arguments to pass to the script.interpreter_args- A string or array of strings composed of arguments to call the interpreter process with. Eg “–harmony” or [”–harmony”,”–debug”]. Only applies ifinterpreteris something other than “none” (its “node” by default).cwd- The working directory to start the process with.out_file(aliasoutput) - (Default:"~/.pm2/logs/app_name-out.log") The path to a file to append stdout output to. Can be the same file aserror_file.error_file(aliaserror) - (Default:"~/.pm2/logs/app_name-error.log") The path to a file to append stderr output to. Can be the same file asout_file.log_date_format- The display format for log timestamps (eg “YYYY-MM-DD HH:mm Z”). The format is a moment display format.pid_file(aliaspid) - (Default:"~/.pm2/pids/app_name-id.pid") The path to a file to write the pid of the started process. The file will be overwritten. Note that the file is not used in any way by pm2 and so the user is free to manipulate or remove that file at any time. The file will be deleted when the process is stopped or the daemon killed.min_uptime- The minimum uptime of the script before it’s considered successfully started.max_restarts- The maximum number of times in a row a script will be restarted if it exits in less thanmin_uptime.max_memory_restart- If sets andscript’s memory usage goes about the configured number, pm2 restarts thescript. Uses human-friendly suffixes: ‘K’ for kilobytes, ‘M’ for megabytes, ‘G’ for gigabytes’, etc. Eg “150M”.kill_timeout- (Default:1600) The number of milliseconds to wait after astoporrestartcommand issues aSIGINTsignal to kill the script forcibly with aSIGKILLsignal.restart_delay- (Default:0) Number of milliseconds to wait before restarting a script that has exited.interpreter- (Default:'node') The interpreter for your script (eg “python”, “ruby”, “bash”, etc). The value “none” will execute the ‘script’ as a binary executable.exec_mode- (Default:'fork') If sets to ‘cluster’, will enable clustering (running multiple instances of thescript). See here for more details.instances- (Default:1) How many instances ofscriptto create. Only relevant inexec_mode‘cluster’.merge_logs- (Default:false) If true, merges the log files for all instances ofscriptinto one stderr log and one stdout log. Only applies in ‘cluster’ mode. For example, if you have 4 instances of ‘test.js’ started via pm2, normally you would have 4 stdout log files and 4 stderr log files, but with this option set to true you would only have one stdout file and one stderr file.watch- If set totrue, the application will be restarted on change of thescriptfile.force(Default:false) By default, pm2 will only start a script if that script isn’t already running (a script is a path to an application, not the name of an application already running). Ifforceis set to true, pm2 will start a new instance of that script.autorestart(Defaulttrue). Iffalse, pm2 will not attempt to restart it following successful completion or process failure.cron_restart(aliascron) - Cron pattern to restart the app.env- Environment variables passed to the app.
The callback receives an array of process objects.
pm2.stop(process, fn)
Stop a process
| Param | Type | Description |
|---|---|---|
| process | string/number | target process ID, Name, ‘all’ or ecosystem file |
| fn | function | Callback |
pm2.restart(process, [options], fn)
Restart a process
| Param | Type | Description |
|---|---|---|
| process | string/number | target process ID, Name, ‘all’ or ecosystem file |
| [options] | object | options (also add updateEnv: true to force update) |
| fn | function | Callback |
pm2.reload(process, [options], fn)
Reload a process
| Param | Type | Description |
|---|---|---|
| process | string/number | target process ID, Name, ‘all’ or ecosystem file |
| [options] | object | updateEnv: true to update the environment, env: 'production' to pick an env_production block (ecosystem file only) |
| fn | function | Callback |
pm2.delete(process, fn)
Delete a process
| Param | Type | Description |
|---|---|---|
| process | string/number | target process ID, Name, ‘all’ or ecosystem file |
| fn | function | Callback |
pm2.killDaemon(fn)
Kills the pm2 daemon (same as pm2 kill, alias pm2.kill(fn)). Note that when the daemon is killed, all its processes are also killed. Also note that you still have to explicitly disconnect from the daemon even after you kill it.
pm2.describe(process, fn)
Get all metadata from a target process
| Param | Type | Description |
|---|---|---|
| process | string/number | target process ID or Name |
| fn | function | Callback |
pm2.list(fn)
Retrieve all processes managed with PM2
Other Methods
All callbacks are fn(err, result).
pm2.scale(name, number, fn)- scale a cluster app tonumberinstances ('+2'/'-1'strings add or remove)pm2.reset(process, fn)- reset restart counterspm2.flush([process], fn)- empty the log files ofprocess, or of all apps andpm2.logwhen omittedpm2.reloadLogs(fn)- reopen all log files (after a rotation)pm2.dump([force], fn)- save the process list to~/.pm2/dump.pm2(pm2 save)pm2.resurrect(fn)- restore the processes saved bydumppm2.startup(platform, opts, fn)/pm2.uninstallStartup(platform, opts, fn)- generate / remove the boot script (see Startup Script)pm2.trigger(process, action_name, [params], fn)- trigger a custom actionpm2.sendLineToStdin(pm_id, line, [separator], fn)- write a line to the app stdinpm2.getPID([name], fn)- pids of all apps or of apps namednamepm2.env(pm_id, fn)- print the environment of an apppm2.getVersion(fn)- version of the running daemonpm2.ping(fn)- ping the daemonpm2.profile('cpu'|'mem', [time], fn)- profile the PM2 daemon fortimems (default 10000), written to the current directorypm2.serve(path, port, opts, {}, fn)- serve a static directory (see Expose static files)pm2.install(module, [opts], fn)/pm2.uninstall(module, fn)- install / remove a PM2 modulepm2.get([key], fn),pm2.set(key, value, fn),pm2.multiset('key1 val1 key2 val2', fn),pm2.unset(key, fn)- PM2 / module configuration (pm2 set)
Custom PM2 instance
require('pm2') is a ready instance using ~/.pm2. require('pm2').custom is the class, to drive another PM2:
const PM2 = require('pm2').custom
const pm2 = new PM2({
pm2_home: '/opt/pm2-home', // PM2 directory (logs, pids, sockets)
cwd: __dirname // cwd used to resolve script paths
})
pm2.connect(function(err) { /* ... */ })
| Option | Default | Description |
|---|---|---|
cwd |
process.cwd() |
cwd used to resolve script paths |
pm2_home |
~/.pm2 |
PM2 directory |
independent |
false |
unique PM2 in a random /tmp folder (cannot be combined with pm2_home) |
daemon_mode |
true (false when independent) |
run the daemon detached or inside the current process |
public_key / secret_key / machine_name |
PM2.io bucket keys and server name |
pm2.destroy(fn)
Kill the daemon and delete its pm2_home folder. Refused on a folder containing .pm2: meant for independent / temporary instances.
Advanced Methods
pm2.sendDataToProcessId([id], packet, fn)
Send data to target process. Also callable as pm2.sendDataToProcessId(id, packet, fn) (sets packet.id). The callback is required.
| Param | Type | Description |
|---|---|---|
| packet.id | number | target process ID |
| packet.type | string | must be process:msg |
| packet.topic | string | any non-empty value (required) |
| packet.data | object | object data that will be sent to target process |
| fn | function | Callback(err, res) |
Data will be received by target process via:
process.on('message', function(packet) {})
pm2.launchBus(fn)
This allow to receive message from process managed with PM2.
const pm2 = require('pm2')
pm2.launchBus(function(err, pm2_bus) {
pm2_bus.on('process:msg', function(packet) {
console.log(packet)
})
})
Events emitted on the bus:
| Event | Payload |
|---|---|
process:msg |
process.send({ type: 'process:msg', data }) from an app (any other type is emitted under that name) |
process:event |
process state change: event (start, online, exit, restart overlimit…), manually, process |
log:out / log:err |
a line written by an app to stdout / stderr |
log:PM2 |
a line logged by the daemon itself |
pm2:kill |
the daemon is being killed |
Then from a process managed with PM2:
process.send({
type : 'process:msg',
data : {
success : true
}
})
pm2.sendSignalToProcessName(signal, process, fn)
Send custom system signal to target process name
| Param | Type | Description |
|---|---|---|
| signal | string | system signal name |
| process | string | target process Name |
| fn | function | Callback(err, process) |
pm2.sendSignalToProcessId(signal, process, fn)
Send custom system signal to target process id
| Param | Type | Description |
|---|---|---|
| signal | string | system signal name |
| process | number | target process id |
| fn | function | Callback(err, process) |
Process structure
When calling any of the above methods, a mutated process array is returned. This object contains:
- processDescription - An array of objects with information about the process. Each object contains the properties:
- name - The name given in the original start command.
- pid - The pid of the process.
- pm_id - PM2’s id of the app (as shown by
pm2 list). - monit - An object containing:
- memory - The number of bytes the process is using.
- cpu - The percent of CPU being used by the process at the moment.
- pm2_env - The list of path variables in the process’s environment. These variables include:
- pm_cwd - The working directory of the process.
- pm_out_log_path - The stdout log file path.
- pm_err_log_path - The stderr log file path.
- exec_interpreter - The interpreter used.
- pm_uptime - The uptime of the process.
- unstable_restarts - The number of unstable restarts the process has been through.
- restart_time
- status - “online”, “stopping”, “stopped”, “launching”, “waiting restart”, “errored”, or “one-launch-status”
- instances - The number of running instances.
- pm_exec_path - The path of the script being run in this process.
Examples
Send message to process
pm2-call.js:
const pm2 = require('pm2')
pm2.connect(function() {
pm2.sendDataToProcessId({
// id of process from "pm2 list" command or from pm2.list(errback) method
id : 0,
// process:msg will be send as 'message' on target process
type : 'process:msg',
// Data to be sent
data : {
some : 'data'
},
topic: 'some topic'
}, function(err, res) {
})
})
// Listen to messages from application
pm2.launchBus(function(err, pm2_bus) {
pm2_bus.on('process:msg', function(packet) {
console.log(packet)
})
})
pm2-app.js:
process.on('message', function(packet) {
process.send({
type : 'process:msg',
data : {
success : true
}
});
});