pm2是一个带有负载均衡功能的应用进程管理器,相似有Supervisor,forever。javascript
Linux Binaries下载地址:https://nodejs.org/distphp
PS: 若是你的主机没法链接公网,先找到能连公网的主机安装上面的方法安装pm2,而后拷贝到你要安装的主机。拷贝以下目录:html
再建立相关软链接java
假设你如今已经写好了一个app.js的文件,须要启动,你可使用pm2进行管理node
默认的目录是:当前用于的家目录下的.pm2目录(此目录能够自定义,请参考:5、自定义启动文件),详细信息以下:python
建立一个test.json的示例文件,格式以下:linux
说明:shell
apps:json结构,apps是一个数组,每个数组成员就是对应一个pm2中运行的应用npm
name:应用程序的名称json
cwd:应用程序所在的目录
script:应用程序的脚本路径
exec_interpreter:应用程序的脚本类型,这里使用的shell,默认是nodejs
min_uptime:最小运行时间,这里设置的是60s即若是应用程序在60s内退出,pm2会认为程序异常退出,此时触发重启max_restarts设置数量
max_restarts:设置应用程序异常退出重启的次数,默认15次(从0开始计数)
exec_mode:应用程序启动模式,这里设置的是cluster_mode(集群),默认是fork
error_file:自定义应用程序的错误日志文件
out_file:自定义应用程序日志文件
pid_file:自定义应用程序的pid文件
watch:是否启用监控模式,默认是false。若是设置成true,当应用程序变更时,pm2会自动重载。这里也能够设置你要监控的文件。
详细参数列表:见附件八
已上面的test.json为例
其余可参数见官网:http://pm2.keymetrics.io
Field | Type | Example | Description |
name | string | "myAPI" | name your app will have in PM2 |
script | string | "bin/app.js" | path of your app |
args | list | ["--enable-logs", "-n", "15"] | arguments given to your app when it is launched |
node_args | list | ["--harmony", "--max-stack-size=1024"] | arguments given to node when it is launched |
cwd | string | "/var/www/app/prod" | the directory from which your app will be launched |
exec_mode | string | "cluster" | "fork" mode is used by default, "cluster" mode can be configured with instances field |
instances | number | 4 | number of instances for your clustered app, 0 means as much instances as you have CPU cores. a negative value means CPU cores - value (e.g -1 on a 4 cores machine will spawn 3 instances) |
exec_interpreter | string | "node" | defaults to "node". can be "python", "ruby", "bash" or whatever interpreter you wish to use. "none" will execute your app as a binary executable |
log_date_format | string | "YYYY-MM-DD HH:mm Z" | format in which timestamps will be displayed in the logs |
error_file | string | "/var/log/node-app/node-app.stderr.log" | path to the specified error log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id> |
out_file | string | "/var/log/node-app/node-app.stdout.log" | path to the specified output log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id> |
pid_file | string | "pids/node-geo-api.pid" | path to the specified pid file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id> |
merge_logs | boolean | false | defaults to false. if true, it will merge logs from all instances of the same app into the same file |
cron_restart | string | "1 0 * * *" | a cron pattern to restart your app. only works in "cluster" mode for now. soon to be avaible in "fork" mode as well |
watch | boolean | true | enables the watch feature, defaults to "false". if true, it will restart your app everytime a file change is detected on the folder or subfolder of your app. |
ignore_watch | list | ["[\/\\]\./", "node_modules"] | list of regex to ignore some file or folder names by the watch feature |
min_uptime | number | 1000 | min uptime of the app to be considered started (i.e. if the app crashes in this time frame, the app will only be restarted the number set in max_restarts (default 15), after that it's errored) |
max_restarts | number | 10 | number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being |
max_memory_restart | string | "150M" | your app will be restarted by PM2 if it exceeds the amount of memory specified. human-friendly format : it can be "10M", "100K", "2G" and so on... |
env | object | {"NODE_ENV": "production", "ID": "42"} | env variables which will appear in your app |
autorestart | boolean | false | true by default. if false, PM2 will not restart your app if it crashes or ends peacefully |
vizion | boolean | false | true by default. if false, PM2 will start without vizion features (versioning control metadatas) |
post_update | list | ["npm install", "echo launching the app"] | a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard |
force | boolean | true | defaults to false. if true, you can start the same script several times which is usually not allowed by PM2 |
next_gen_js | boolean | true | defaults to false. if true, PM2 will launch your app using embedded BabelJS features which means you can run ES6/ES7 javascript code |
restart_delay | number | 4000 | time to wait before restarting a crashed app (in milliseconds). defaults to 0. |