pm2 是一個帶有負載均衡功能的Node應用的進程管理器
當你要把你的獨立代碼利用所有的服務器上的全部CPU,並保證進程永遠都活着,0秒的重載, PM2是完美的
它很是適合IaaS結構,但不要把它用於PaaS方案(隨後將開發Paas的解決方案)python
內建負載均衡(使用Node cluster 集羣模塊)
後臺運行
0秒停機重載,我理解大概意思是維護升級的時候不須要停機.
具備Ubuntu和CentOS 的啓動腳本
中止不穩定的進程(避免無限循環)
控制檯檢測
提供 HTTP API
遠程控制和實時的接口API ( Nodejs 模塊,容許和PM2進程管理器交互 )git
npm install -g pm2
pm2 start app.js -i 4 #後臺運行pm2,啓動4個app.js # 也能夠把'max' 參數傳遞給 start # 正確的進程數目依賴於Cpu的核心數目
pm2 start app.js --name 「my-api」 # 命名進程
[tomcat@CESHI-001 ~]$ pm2 list ┌─────────────┬─────────┬────────┬───┬─────┬───────────┐ │ Name│ mode│ status │ ↺│ cpu │ memory│ ├─────────────┼─────────┼────────┼───┼─────┼───────────┤ │ cwxx_wechat │ cluster │ online │ 0 │ 0% │ 31.1 MB │ │ cwxx_wechat │ cluster │ online │ 0 │ 0% │ 31.8 MB │ │ cwxx_wechat │ cluster │ online │ 0 │ 0% │ 31.1 MB │ │ cwxx_wechat │ cluster │ online │ 0 │ 0% │ 29.4 MB │ └─────────────┴─────────┴────────┴───┴─────┴───────────┘ Use `pm2 show <id|name>` to get more details about an app
注意:
使用pm2 list 查看狀態的時候,若是你使用的是tomcat用戶啓動的pm2。此時必須使用tomcat用戶才能看到pm2監控的狀態。若是使用root用戶是看不到的會顯示以下問題github
[root@CESHI-001 ~]# pm2 list ┌──────┬──────┬────────┬───┬─────┬────────┐ │ Name │ mode │ status │ ↺ │ cpu │ memory │ └──────┴──────┴────────┴───┴─────┴────────┘ Use `pm2 show <id|name>` to get more details about an app
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秒停機重載web
這項功能容許你從新載入代碼而不用失去請求鏈接。
注意:
僅能用於web應用
運行於Node 0.11.x版本
運行於 cluster 模式(默認模式)
npm
pm2 reload all
CoffeeScript:
pm2 start my_app.coffee #這就是所有
只需在你的服務器上測試
json
git clone https://github.com/Unitech/pm2.git cd pm2 npm install # 或者 npm install --dev ,若是devDependencies 沒有安裝 npm test