pm2是一个高级node进程管理工具,通过它不仅可以管理node应用,也可以管理Python等其他语言的应用;本文是看完官网后做的笔录,适合快速入门pm2命令行工具的使用,具体细节可参考官网介绍
pm2使用
快速使用
安装
1 | npm install pm2@latest -g |
使用
1 | pm2 start app.js |
通过配置文件管理多个应用
1 | # 创建 process.yml内容如下: |
与pm2相关的所有配置位于$HOME/.pm2下
- logs 应用log
- pids 应用pid
- sock 套接字文件
- conf.js 配置文件
更新
1 | # 全局安装 |
命令清单
1 | # Fork mode |
启动其他语言应用
1 | # 启动其他语言的进程 |
控制应用内存大小
1 | # 启动应用 显示内存大小 |
集群模式
根据cpus个数生成多个应用实例,提高应用的性能及可靠性
1 | pm2 start app.js -i max |
应用平滑关闭
1 | process.on('SIGINT', function() { |
});
启动配置文件
配置文件格式
- javascript
- json
- yaml
生成配置文件
1 | # 生成 ecosystem.config.js |
JavaScript格式
1 | module.exports = { |
json格式
1 | { |
yaml格式
1 | apps: |
根据配置文件管理应用
1 | pm2 start ecosystem.config.js |
更新环境变量信息
1 | pm2 restart ecosystem.config.js --update-env |
配置参数列表
配置参数列表
Field | Type | Example | Description |
---|---|---|---|
name | (string) | “my-api” | application name (default to script filename without extension) |
script | (string) | ”./api/app.js” | script path relative to pm2 start |
cwd | (string) | “/var/www/” | the directory from which your app will be launched |
args | (string) | “-a 13 -b 12” | string containing all arguments passed via CLI to script |
interpreter | (string) | “/usr/bin/python” | interpreter absolute path (default to node) |
interpreter_args | (string) | ”–harmony” | option to pass to the interpreter |
node_args | (string) | alias to interpreter_args | |
instances | number | -1 | number of app instance to be launched |
exec_mode | string | “cluster” | mode to start your app, can be “cluster” or “fork”, default fork |
watch | boolean or [] | true | enable watch & restart feature, if a file change in the folder or subfolder, your app will get reloaded |
ignore_watch | list | [”[\/\]./”, “node_modules”] | list of regex to ignore some file or folder names by the watch feature |
max_memory_restart | string | “150M” | your app will be restarted if it exceeds the amount of memory specified. human-friendly format : it can be “10M”, “100K”, “2G” and so on… |
env | object | {“NODE_ENV”: “development”, “ID”: “42”} | env variables which will appear in your app |
env_<env_name></env_name> | object | {“NODE_ENV”: “production”, “ID”: “89”} | inject <env_name> when doing pm2 restart app.yml –env <env_name></env_name></env_name> |
source_map_support | boolean | true | default to true, [enable/disable source map file] |
instance_var | string | “NODE_APP_INSTANCE” | see documentation |
log_date_format | (string) | “YYYY-MM-DD HH:mm Z” | log date format (see log section) |
error_file | (string) | error file path (default to $HOME/.pm2/logs/XXXerr.log) | |
out_file | (string) | output file path (default to $HOME/.pm2/logs/XXXout.log) | |
combine_logs | boolean | true | if set to true, avoid to suffix logs file with the process id |
merge_logs | boolean | true | alias to combine_logs |
pid_file | (string) | pid file path (default to $HOME/.pm2/pid/app-pm_id.pid) | |
min_uptime | (string) | min uptime of the app to be considered started | |
listen_timeout | number | 8000 | time in ms before forcing a reload if app not listening |
kill_timeout | number | 1600 | time in milliseconds before sending a final SIGKILL |
wait_ready | boolean | false | Instead of reload waiting for listen event, wait for process.send(‘ready’) |
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 restarted |
restart_delay | number | 4000 | time to wait before restarting a crashed app (in milliseconds). defaults to 0. |
autorestart | boolean | false | true by default. if false, PM2 will not restart your app if it crashes or ends peacefully |
cron_restart | string | “1 0 *” | a cron pattern to restart your app. Application must be running for cron feature to work |
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 |
使用json配置文件的方式,命令行可选参数会无效
日志文件
1 | # 不保存日志 |
状态管理
平滑停止
1 | # Graceful restart/reload/stop |
平滑启动
某些应用运行需要等待DB或者workers建立好链接
1 | # 应用发出ready正式更新状态 |
环境变量
1 | # 1.PM2 默认注入当前shell的环境变量 |
应用日志管理
PM2日志可以合并,也可以存放不同的日志文件中。
实时显示日志
1 | # 查看logs命令帮助 |
更新PM2
1 | # 保存当前进程信息 |
部署
本地我们可以直接通过 pm2 start/stop管理本地应用,通过deploy命令,在本地可以很轻松的将本地的应用部署远程服务器上。
1 | { |
将pm2设置成系统服务
- systemd: Ubuntu >= 16, CentOS >= 7, Arch, Debian >= 7
- upstart: Ubuntu <= 14
- launchd: Darwin, MacOSx
- openrc: Gentoo Linux, Arch Linux
- rcd: FreeBSD
- systemv: Centos 6, Amazon Linux
1 | pm2 startup |
检测自启服务是否设置好
1 | # Check if pm2-<USER> service has been added |
自动重启服务
1 | # 当前目录或子目录文件改动自动重启服务 |
sourcemap
当使用babeljs,typescript时,有了sourcemap,异常了可以找到具体错误信息。
1 | # 命令行指定 |
静态服务
pm2 2.4.0 后可使用静态服务命令
1 | # 命令行方式 |