PM2使用配置

Posted by yuk on 2016-03-30

PM2 是一个带有负载均衡功能的 Node 应用的进程管理器。

安装

1
npm install -g pm2

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ npm install pm2 -g     # 命令行安装 pm2 
$ pm2 start app.js -i 4 # 后台运行pm2,启动4个app.js
# 也可以把'max' 参数传递给 start
# 正确的进程数目依赖于Cpu的核心数目
$ pm2 start app.js --name my-api # 命名进程
$ pm2 list # 显示所有进程状态
$ pm2 monit # 监视所有进程
$ pm2 logs # 显示所有进程日志
$ pm2 stop all # 停止所有进程
$ pm2 restart all # 重启所有进程
$ pm2 reload all # 0 秒停机重载进程 (用于 NETWORKED 进程)
$ pm2 stop 0 # 停止指定的进程
$ pm2 restart 0 # 重启指定的进程
$ pm2 startup # 产生 init 脚本 保持进程活着
$ pm2 web # 运行健壮的 computer API endpoint (http://localhost:9615)
$ pm2 delete 0 # 杀死指定的进程
$ pm2 delete all # 杀死全部进程

运行进程的不同方式

1
2
3
4
5
6
7
8
9
10
11
12
$ pm2 start app.js -i max    # 根据有效CPU数目启动最大进程数目
$ pm2 start app.js -i 3 # 启动3个进程
$ pm2 start app.js -x #用fork模式启动 app.js 而不是使用 cluster
$ pm2 start app.js -x -- -a 23 # 用fork模式启动 app.js 并且传递参数 (-a 23)
$ pm2 start app.js --name serverone # 启动一个进程并把它命名为 serverone
$ pm2 stop serverone # 停止 serverone 进程
$ pm2 start app.json # 启动进程, 在 app.json里设置选项
$ pm2 start app.js -i max -- -a 23 #在--之后给 app.js 传递参数
$ pm2 start app.js -i max -e err.log -o out.log # 启动 并 生成一个配置文件
#你也可以执行用其他语言编写的app ( fork 模式):
$ pm2 start my-bash-script.sh -x --interpreter bash
$ pm2 start my-python-script.py -x --interpreter python

。。。
我想使用 pm2 启动 babel-node 然后又支持 –harmony模式
写一个启动json

1
2
3
4
5
6
7
8
9
10
{
"name" : "yuk-admin",
"script" : "app.js",
"error_file" : "log/app-err.log",
"out_file" : "log/app-out.log",
"watch" : false,
"exec_interpreter" : "babel-node",
"node_args": "--harmony",
"exec_mode" : "fork"
}

发现并不能启动,主要是没有预设 “presets”: [“es2015”]。
后来在官网中发现这么一句话

I don’t know about the require hook, but apparently using babel-node in production is not recommended:
反正就是不推荐在生产环境中使用babel-node方式