[转] forever 替代工具 pm2 的介绍
发布于 11 年前 作者 jiyinyiyong 27582 次浏览 最后一次编辑是 8 年前

[Goodbye node-forever, hello PM2][pm2] [pm2]: (http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html)

看了工具的介绍, 觉得蛮方便的, 就是不支持 CoffeeScript 让人有点抓狂:

Github: https://github.com/Unitech/pm2

npm install -g pm2

特点

PM2 觉得 Forever 有一些不好用的地方:

  • log 和监视的功能有限
  • 进程和集群管理有限
  • Aging codebase (升级 Node 容易导致应用失败)

目测比 Forever 好用一些… PM2 是前几个月刚写的, 还不完善: 列出来的计划有:

  • Remote administration/status checking,
  • Built-in inter-processes communication channel (message bus),
  • V8 GC 内存泄露检测,
  • Web 界面,
  • Persistent storage for monitoring data,
  • 邮件通知

Demo

盗链个截图:

然后本地安装尝试了一下:

➤➤ cat a.js 
// Generated by CoffeeScript 1.6.3
require('http').createServer(function(req, res) {
  return res.end('ok');
}).listen(3000);

➤➤ pm2 start a.js 
⌬ PM2 Configuration :  { script: 'a.js', name: 'a' }
⌬ PM2 You can write the current configuration by adding -w option
⌬ PM2 Process launched
┌────────┬────┬───────┬────────┬───────────┬─────────────────────┬──────────┬────────────────────────────────┐
│ Script │ id │ PID   │ status │ Restarted │ Last restart        │   memory │ err logs                       │
├────────┼────┼───────┼────────┼───────────┼─────────────────────┼──────────┼────────────────────────────────┤
│ a.js   │ 0  │ 27515 │ online │ 0         │ 2013-06-27 14:16:28 │ 8.469 MB │ /home/yong/.pm2/logs/a-err.log │


➤➤ pm2 list
⌬ PM2  Process listing
┌────────┬────┬───────┬────────┬───────────┬─────────────────────┬───────────┬────────────────────────────────┐
│ Script │ id │ PID   │ status │ Restarted │ Last restart        │    memory │ err logs                       │
├────────┼────┼───────┼────────┼───────────┼─────────────────────┼───────────┼────────────────────────────────┤
│ a.js   │ 0  │ 27515 │ online │ 0         │ 2013-06-27 14:16:28 │ 10.102 MB │ /home/yong/.pm2/logs/a-err.log │
└────────┴────┴───────┴────────┴───────────┴─────────────────────┴───────────┴────────────────────────────────┘
⌬ PM2  PM2 log file path :  /home/yong/.pm2/pm2.log  (type pm2 logs to see log streaming)

⌬ PM2 monitoring :

a.js [27515]                           [                              ] 0 %
                                       [|||                           ] 10.102 MB

➤➤ pm2 kill
Daemon killed

➤➤ pm2 logs
Launching daemon
{ online: true, success: true, pid: 27553 }
[PM2 DAEMON (l0)] "/opt/s/demo/a.coffee -省略省略...
17 回复

我的是windows XP系统,我用的是msysgit,结果在
npm install -g pm2
的时候,报我没有安装git,我去,啊。。多么痛的领悟…

github有windows客户端

卸載Windows ^o^

确实比forever功能丰富些

在Windows 7下一次就安装成功,爽了一下,于是运行看看:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

x:\>pm2 list

path.js:116
        throw new TypeError('Arguments to path.resolve must be strings');
              ^
TypeError: Arguments to path.resolve must be strings
    at Object.exports.resolve (path.js:116:15)
    at Object.<anonymous> (C:\Users\ooxx\AppData\Roaming\npm\node_modules\pm2\constants.js:8:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\Users\ooxx\AppData\Roaming\npm\node_modules\pm2\lib\Satan.js:11:11)
    at Module._compile (module.js:456:26)

多么痛的领悟…

@haoxin365 我安装了windows客户端的git,使用起来也还是不行,气得我直接迁移到ubuntu上面了,使用起来舒服多了

@leizongmin 是不是不支持Windows的原因啊?

pm2 的stop 程序停了,端口还在占用,何解?

目测只能pm2 kill

不了解, 怎么重现这个?

@jiyinyiyong

随便写段代码,监听某个端口 pm2 start xxx.js pm2 stop xxx.js node xxx.js

好东西,马了

无解,这也是pm2非常烦的一点。我们只在线上环境使用pm2,测试和预发,都用forever。端口占用很头疼。

我们自己写了一个WEB管理控制台,deamon用的是cluster模式实现的,还写了一点IPC通信,这样登录后台可以看到各子进程的PID、工作状态、内存占用、数据库连接占用、连接排队、HTTP会话数、TCP会话数、Cache使用率、Cache命中率等,管理控制台还记录了每个业务模块的调用次数及调用耗时用于性能调优,deamon进程使用node-schedule每10分钟记录一次子进程服务状态同时生成监控页面再通过shell汇总和压缩。好像我们自己有点重复造轮子的感觉。

@getpro 我经常遇到这种问题 pm2 stop 然后在 pm2 kill 端口还在占用 就只好重启服务器了

@xinyu198736 :-D,看来开发还是forever稳定一些啊

回到顶部