文章主要来自豆瓣上一位同窗的文章:PM2 介绍,中间可能有部分修改!node
PM2 是一个带有负载均衡功能的 Node 应用的进程管理器。python
当你要把你的独立代码利用所有的服务器上的全部 CPU,并保证进程永远都活着,0 秒的重载, PM2 是完美的。它很是适合 IaaS 结构,但不要把它用于 PaaS 方案(随后将开发 Paas 的解决方案)。git
备注:github
请参考web
云服务模式:SaaS、PaaS 和 IaaS,哪种适合你?npm
测试过 Nodejs v0.11/v0.10/v0.8 版本,兼容 CoffeeScript,基于 Linux 和 MacOS。json
npm install -g pm2
$ 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 # 杀死所有进程
$ 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
0 秒停机重载:这项功能容许你从新载入代码而不用失去请求链接。api
$ pm2 reload all
$ pm2 start my_app.coffee #这就是所有
只需在你的服务器上测试bash
$ git clone https://github.com/Unitech/pm2.git $ cd pm2 $ npm install # 或者 npm install --dev ,若是devDependencies 没有安装 $ npm test
pm2 list
:列出由 PM2 管理的全部进程信息,还会显示一个进程会被启动多少次,由于没处理的异常。服务器
pm2 monit
:监视每一个 node 进程的 CPU 和内存的使用状况。
进入项目目录,执行如下命令
pm2 start app.js
会出现如下错误信息,该如何解决呢?
PM2 | 2017-10-26 10:45:21: Starting execution sequence in -fork mode- for app name:app id:0 PM2 | 2017-10-26 10:45:21: App name:app id:0 online PM2 | 2017-10-26 10:45:21: App [app] with id [0] and pid [17365], exited with code [0] via signal [SIGINT] PM2 | 2017-10-26 10:45:21: Starting execution sequence in -fork mode- for app name:app id:0 PM2 | 2017-10-26 10:45:21: App name:app id:0 online PM2 | 2017-10-26 10:45:22: App [app] with id [0] and pid [17375], exited with code [0] via signal [SIGINT] PM2 | 2017-10-26 10:45:22: Starting execution sequence in -fork mode- for app name:app id:0 PM2 | 2017-10-26 10:45:22: App name:app id:0 online PM2 | 2017-10-26 10:45:22: App [app] with id [0] and pid [17385], exited with code [0] via signal [SIGINT] PM2 | 2017-10-26 10:45:22: Starting execution sequence in -fork mode- for app name:app id:0 PM2 | 2017-10-26 10:45:22: App name:app id:0 online PM2 | 2017-10-26 10:45:23: App [app] with id [0] and pid [17395], exited with code [0] via signal [SIGINT] PM2 | 2017-10-26 10:45:23: Script /home/nodeWork/regalcoin/app.js had too many unstable restarts (16). Stopped. "errored" PM2 | 2017-10-26 10:50:51: Starting execution sequence in -fork mode- for app name:www id:1 PM2 | 2017-10-26 10:50:51: App name:www id:1 online
在当前项目目录,执行
pm2 start ./bin/www
成功运行起来
启动项目
pm2 start www(App name)
中止项目
pm2 stop www