详情可以参见: https://wohugb.gitbooks.io/pm2/content/features/quick-start.html
启动:
$ pm2 start web.js --name "web-interface" //执行web.js 服务名是web-interface
停止:
$ pm2 stop web-interface //服务还存在但是已经处于stopped状态
重启:
$ pm2 restart web-interface
删除:
$ pm2 delete web-interface
列表:
$ pm2 list
查看列表中的详情
$ pm2 describe [server_name]
$ pm2 show 0 // 0=>pid list里面的id
重启临界内存设置
PM2 允许基于内存占用来重启应用
CLI
$ pm2 start big-array.js --max-memory-restart 20M
JSON
{
"name" : "max_mem",
"script" : "big-array.js",
"max_memory_restart" : "20M" }
编程
pm2.start({
name : "max_mem",
script : "big-array.js",
max_memory_restart : "20M"
}, function(err, proc) {
// Processing
});
单位
单位可以是 K(ilobyte), M(egabyte), G(igabyte).
50M
50K
1G
监控CPU/内存使用率
监控启动的所有进程:
$ pm2 monit
实时显示日志
$ pm2 logs //显示日志
$ pm2 flush # Clear all the logs //清除日志
$
pm2 dump #导出日志
集群 (cluster_mode)
$ pm2 start app.js -i 1 // To enable the cluster_mode, just pass the -i option ,开启一个进程
$ pm2 start app.js -i max # 根据机器CPU核数,开启对应数目的进程
热重载
$ pm2 reload api #[only used in cluster mode]
优雅重载
mechanism that will send a shutdown message to your process before reloading it ; shutdown via the PM2_GRACEFUL_TIMEOUT
environment variable;
启动脚本
$ pm2 startup [ubuntu|centos|gentoo|systemd]
$ pm2 save
$ pm2 dump #save startup status into file
JSON应用声明
在processes.json定义应用参数:
然后运行:
$ pm2 start processes.json
$ pm2 stop processes.json
$ pm2 delete processes.json
$ pm2 restart processes.json