1 安装
安装最新版本pm2
npm install pm2@latest -g
2 开始一个应用
pm2 start app.js
常用命令:
# Listing
pm2 list # Display all processes status
pm2 jlist # Print process list in raw JSON
pm2 prettylist # Print process list in beautified JSON
pm2 describe 0 # Display all informations about a specific process
pm2 monit # Monitor all processes
3 日志管理
3.1 日志管理常用命令
# Display option for pm2 logs command
pm2 logs -h
# Display all apps logs
pm2 logs
# Display only logs about process containing "api" in their name
pm2 logs /api/
# It's a regex so you can filter with the normal syntax to filter with OR condition
pm2 logs /server_[12]/
# Display only api app logs
pm2 logs api
# Display X lines of api log file
pm2 logs big-api --lines 1000
# Empty all current application logs managed by PM2:
pm2 flush # Clear all the logs
3.2 日志输出格式
以Json类型查看:
pm2 logs --json
以Json类型输出:
CLI : --log-type json
Process file : "log_type": "json"
Config:
{
"message": "echo\n", // the actual message that has been `console.log`
"timestamp": "2017-02-06T14:51:38.896Z", // timestamp of the message, can be formated
"type": "out", // the type of logs, can be `err`, `out` or `PM2`
"process_id": 0, // the process id used by PM2
"app_name": "one-echo" // the application name
}
3.3 日志管理插件
安装
pm2 install pm2-logrotate
回滚设置
pm2 set pm2-logrotate:max_size 1K # (1KB)
pm2 set pm2-logrotate:compress true # (compress logs when rotated)
pm2 set pm2-logrotate:rotateInterval '*/1 * * * *' # (force rotate every minute)
3.4 日志配置
{
"script" : "echo.js",
"error_file" : "err.log",
"out_file" : "out.log",
"merge_logs" : true,
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}
取消日志
{
"out_file": "/dev/null",
"error_file": "/dev/null"
}
4 启动文件配置
可以通过配置文件启动:
配置文件 app.conf.yaml
:
apps:
- script : ./test_rmq.js
name : 'sendmsg1'
instances: 1
exec_mode: 'fork'
error_file: /dev/null
out_file : /dev/null
args : 'extrainfo1'
- script : ./test_rmq.js
name : 'sendmsg2'
instances: 1
exec_mode: 'fork'
error_file: /dev/null
out_file : /dev/null
args : 'extrainfo2'
其中args为启动参数。启动所有程序:
pm2 start app.conf.yaml
仅启动其中一个应用:
pm2 start app.conf.yaml --only sendmsg1
这些只是pm2的简单应用, 具体的api查阅官方文档:http://pm2.keymetrics.io/docs/usage/quick-start/
网友评论