【进程管理器】PM2应用进程管理器

1. pm2简介

pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:http://pm2.keymetrics.io

gitbook:https://wohugb.gitbooks.io/pm2/content/index.html

2. pm2部署

在安装pm2之前需要先部署nodejs,如果没有安装可以参考:nodejs服务安装

2.1. 修改npm仓库

npm默认使用的npm仓库在国外,我们需要修改为国内npm仓库

~]# npm config get registry
https://registry.npmjs.org/
~]# npm config set registry https://registry.npm.taobao.org
~]# npm config get registry
https://registry.npm.taobao.org/

2.2. 安装pm2

~]# npm install -g pm2
~]# pm2 -v
3.0.4

3. pm2常用命令

假设你现在已经写好了一个app.js的文件,需要启动,你可以使用pm2进行管理

3.1. 启动

# pm2 start app.js
# pm2 start app.js --name my-api # my-api为PM2进程名称
# pm2 start app.js -i 0          # 根据CPU核数启动进程个数
# pm2 start app.js --watch       # 实时监控app.js的方式启动,当app.js文件有变动时,pm2会自动reload

3.2. 查看进程

# pm2 list
# pm2 show 0 或者 # pm2 info 0  # 查看进程详细信息,0为PM2进程id 

3.3. 监控

# pm2 monit

3.4. 停止

# pm2 stop all  # 停止PM2列表中所有的进程
# pm2 stop 0    # 停止PM2列表中进程为0的进程

3.5. 重载

# pm2 reload all # 重载PM2列表中所有的进程
# pm2 reload 0   # 重载PM2列表中进程为0的进程

3.6. 重启

# pm2 restart all  # 重启PM2列表中所有的进程
# pm2 restart 0    # 重启PM2列表中进程为0的进程

3.7. 删除PM2进程

# pm2 delete 0    # 删除PM2列表中进程为0的进程
# pm2 delete all  # 删除PM2列表中所有的进程

3.8. 日志操作

# pm2 logs [--raw]  # Display all processes logs in streaming
# pm2 flush         # Empty all log file
# pm2 reloadLogs    # Reload all logs

3.9. 升级PM2

# npm install pm2@lastest -g # 安装最新的PM2版本
# pm2 updatePM2              # 升级pm2

3.10. 更多命令参数请查看帮助

# pm2 --help

4. PM2目录结构

默认的目录是:当前用于的家目录下的.pm2目录(此目录可以自定义),详细信息如下:

$HOME/.pm2                   # will contain all PM2 related files
$HOME/.pm2/logs              # will contain all applications logs
$HOME/.pm2/pids              # will contain all applications pids
$HOME/.pm2/pm2.log           # PM2 logs
$HOME/.pm2/pm2.pid           # PM2 pid
$HOME/.pm2/rpc.sock          # Socket file for remote commands
$HOME/.pm2/pub.sock          # Socket file for publishable events
$HOME/.pm2/conf.js           # PM2 Configuration

5. 测试PM2

5.1. 自定义启动文件

创建一个test.json的示例文件,格式如下:

~]# mkdir -p /opt/htdocs/testapp
~]# cd /opt/htdocs/testapp
testapp]# cat test.json
{
    "apps": [
        {
            "name": "test",
            "cwd": "/opt/htdocs/testapp",
            "script": "./test.sh",
            "exec_interpreter": "bash",
            "log_date_format": "YYYY-MM-DD HH:mm Z",
            "error_file": "./test-err.log",
            "out_file": "./test-out.log",
            "pid_file": "./test.pid",
            "instances": 6,
            "min_uptime": "200s",
            "max_restarts": 10,
            "max_memory_restart": "1M",
            "cron_restart": "1 0 * * *",
            "watch": false,
            "merge_logs": true,
            "exec_mode": "fork",
            "autorestart": false,
            "vizion": false
        }
    ]
}

参数说明:

apps:json结构,apps是一个数组,每一个数组成员就是对应一个pm2中运行的应用
name:应用程序名称
cwd:应用程序所在的目录
script:应用程序的脚本路径
log_date_format:配置日志的输出格式
error_file:自定义应用程序的错误日志文件六、实例展示
out_file:自定义应用程序日志文件已上面的test.json为例
pid_file:自定义应用程序的pid文件
instances:启动脚本相 默认 nodejs
min_uptime:最小运行时间,这里设置的是60s即如果应用程序在60s内退出,pm2会认为程序异常退出,此时触发重启max_restarts设置数量
max_restarts:设置应用程序异常退出重启的次数,默认15次(从0开始计数
cron_restart:定时启动,解决重启能解决的问题
watch:是否启用监控模式,默认是false。如果设置成true,当应用程序变动时,pm2会自动重载。这里也可以设置你要监控的文件。done
merge_logs:combine_logs的别名
exec_interpreter:应用程序的脚本类型,这里使用的shell,默认是nodejs
exec_mode:应用程序启动模式,这里设置的是cluster_mode(集群),默认是fork
autorestart:启用/禁用应用程序崩溃或退出时自动重启
vizion:启用/禁用vizion特性(版本控制)

然后执行如下命令:

testapp]# pm2 start test.json 
[PM2][WARN] You are starting 6 processes in fork_mode without load balancing. To enable it remove -x option.
[PM2][WARN] Applications test not running, starting...
[PM2] cron restart at 1 0 * * *
[PM2] App [test] launched (6 instances)
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────┬──────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤
│ test     │ 0  │ fork │ 2194 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 1  │ fork │ 2196 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 2  │ fork │ 2198 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 3  │ fork │ 2200 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 4  │ fork │ 2202 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 5  │ fork │ 2204 │ online │ 0       │ 0s     │ 0%  │ 1.4 MB   │ root │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴──────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[root@bogon testapp]# pm2 list
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────┬──────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤
│ test     │ 0  │ fork │ 2221 │ online │ 1       │ 5s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 1  │ fork │ 2225 │ online │ 1       │ 5s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 2  │ fork │ 2229 │ online │ 1       │ 5s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 3  │ fork │ 2233 │ online │ 1       │ 5s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 4  │ fork │ 2237 │ online │ 1       │ 4s     │ 0%  │ 1.4 MB   │ root │ disabled │
│ test     │ 5  │ fork │ 2241 │ online │ 1       │ 4s     │ 0%  │ 1.4 MB   │ root │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴──────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
testapp]# pm2 delete all
[PM2] Applying action deleteProcessId on app [all](ids: 0,1,2,3,4,5)
[PM2] [test](3)[PM2] [test](1)[PM2] [test](0)[PM2] [test](2)[PM2] [test](4)[PM2] [test](5) ✓
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[root@bogon testapp]# pm2 list      
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值