Node應用進程管理器pm2的使用

本文轉載自:豆瓣-PM2介紹 
更多內容見github上的pm2說明文檔pm2python

 

pm2 是一個帶有負載均衡功能的Node應用的進程管理器. 
當你要把你的獨立代碼利用所有的服務器上的全部CPU,並保證進程永遠都活着,0秒的重載, PM2是完美的。它很是適合IaaS結構,但不要把它用於PaaS方案(隨後將開發Paas的解決方案).git

備註: 
SaaS、PaaS和IaaS是雲服務模式。 
SaaS 軟件即服務,例如Google的 Gmail 郵箱服務.面向應用型用戶. 
PaaS 平臺即服務.例如Google的GAE,面向開發型用戶 
IaaS 基礎架構即服務,例如亞馬遜的AWS,IaaS對於不知道新推出的應用程序/網站會有多成功的創業公司來講很是有用 
請參考 雲服務模式:SaaS、PaaS和IaaS,哪種適合你?github

 

主要特性: 
* 內建負載均衡(使用Node cluster 集羣模塊) 
* 後臺運行 
* 0秒停機重載,我理解大概意思是維護升級的時候不須要停機. 
* 具備Ubuntu和CentOS 的啓動腳本 
* 中止不穩定的進程(避免無限循環) 
* 控制檯檢測 
* 提供 HTTP API 
* 遠程控制和實時的接口API ( Nodejs 模塊,容許和PM2進程管理器交互 )web

安裝 
npm install -g pm2npm

用法json

$ 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         # 殺死所有進程

運行進程的不一樣方式:api

$ 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應用 
運行於Node 0.11.x版本 
運行於 cluster 模式(默認模式) 
$ pm2 reload allbash

個人項目啓動(前提定位到項目根目錄執行命令):服務器

 pm2 start ./bin/www -i 0 --name 'api' 
相關文章
相關標籤/搜索