PM2 API
PM2 can be used programmatically, allowing to manage processes straight from the code.PM2可以编程使用,允许直接从代码管理流程。
Quickstart快速上手
Note: To release connection to PM2 and make your application auto exit, make sure to disconnect from pm2 with 注意:要释放与PM2的连接并使应用程序自动退出,请确保使用pm2.disconnect()pm2.disconnect()断开与PM2的连接
First add PM2 as a dependency:首先添加PM2作为依赖项:
npm install pm2 --saveThen create a script called app.js and pm2-control.js containing this:然后创建一个名为app.js和pm2-control.js的脚本,其中包含以下内容:
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这将产生或连接到本地PM2Then start app.js with name api然后用名称api启动app.jsDisplay all applications managed with PM2显示使用PM2管理的所有应用程序Then restart the app with name api然后用名称api重新启动应用程序And disconnect from PM2并从PM2上断开
API Methods方法
pm2.connect([no_daemon_mode], fn)
Connect to local PM2 or spawn a new PM2 instance.连接到本地PM2或生成新的PM2实例。
| [no_daemon_mode] | boolean | false | |
| fn | function |
noDaemonMode:If true is passed for the first argument, pm2 will not be run as a daemon and will die when the related script exits.如果为第一个参数传递true,则pm2将不会作为守护进程运行,并且在相关脚本退出时将终止。By default, pm2 stays alive after your script exits.默认情况下,pm2在脚本退出后保持活动状态。If pm2 is already running, your script will link to the existing daemon but will die once your process exits.如果pm2已经在运行,那么脚本将链接到现有的守护进程,但一旦进程退出,脚本就会死掉。
pm2.disconnect()
Disconnect from local PM2从本地PM2上断开
pm2.start(process, fn)
Start a process开始一个进程
| process | string/object | |
| fn | function |
pm2.stop(process, fn)
Stop a process停止进程
| process | string/number | |
| fn | function |
pm2.restart(process, [options], fn)
Restart a process重新启动进程
| process | string/number | |
| [options] | object | optionsupdateEnv:true以强制更新) |
| fn | function |
pm2.reload(process, fn)
Reload a process重新加载进程
| process | string/number | |
| fn | function |
pm2.delete(process, fn)
Delete a process删除进程
| process | string/number | |
| fn | function |
pm2.killDaemon(fn)
Kills the pm2 daemon (same as pm2 kill). 杀死pm2守护进程(与pm2杀死相同)。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从目标进程获取所有元数据
| process | string/number | |
| fn | function |
pm2.list(fn)
Retrieve all processes managed with PM2检索使用PM2管理的所有流程
Advanced Methods先进的方法
pm2.sendDataToProcessId(packet)
Send data to target process.将数据发送到目标进程。
| packet.id | number | |
| packet.type | string | process:msg |
| packet.topic | boolean | true |
| packet.data | object |
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.这允许从PM2管理的流程接收消息。
const pm2 = require('pm2')
pm2.launchBus(function(err, pm2_bus) {
pm2_bus.on('process:msg', function(packet) {
console.log(packet)
})
})Then from a process managed with PM2:然后从PM2管理的流程中:
process.send({
type : 'process:msg',
data : {
success : true
}
})pm2.sendSignalToProcessName(signal, process, fn)
Send custom system signal to target process name向目标进程名称发送自定义系统信号
| signal | string | |
| process | string | |
| fn | function | Callback(err, process) |
pm2.sendSignalToProcessId(signal, process, fn)
Send custom system signal to target process id向目标进程id发送自定义系统信号
| 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.原始start命令中给出的名称。 - pid -
The pid of the process.过程的pid。 - pm_id -
The pid for the pm2 God daemon process.pm2 God守护进程的pid。 - 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.进程当前使用的CPU百分比。
- memory -
- 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.stdout日志文件路径。 - pm_err_log_path -
The stderr log file path.stderr日志文件路径。 - 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”, “errored”, or “one-launch-status”“online”、“stopping”、“stopped”、“launching”、“errored”或“one-launch-status” - instances -
The number of running instances.正在运行的实例数。 - pm_exec_path -
The path of the script being run in this process.在此进程中运行的脚本的路径。
- pm_cwd -
- name -
Examples例子
Send message to process向进程发送消息
pm2-call.js:
const pm2 = require('pm2')
pm2.connect(function() {
pm2.sendDataToProcessId({
// id of procces from "pm2 list" command or from pm2.list(errback) method
id : 1,
// process:msg will be send as 'message' on target process
type : 'process:msg',
// Data to be sent
data : {
some : 'data'
},
topic: true
}, 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
}
});
});