最近把DoraCMS遷移到的linux上,由於node使用npm start開啟服務,當ssh關閉,進程就斷了,另外我在服務器上掛了3個node服務,管理起來非常不方便,於是朋友介紹了pm2,目前我只用到了一小部分功能,已經感覺很強大了。
pm2 是一個帶有負載均衡功能的Node應用的進程管理器. 當你要把你的獨立代碼利用全部的服務器上的所有CPU,並保證進程永遠都活着,0秒的重載, PM2是非常不錯的選擇的。它非常適合IaaS結構,但不要把它用於PaaS方案(隨后將開發Paas的解決方案).
主要特性:
內建負載均衡(使用Node cluster 集群模塊)
后台運行
0秒停機重載,我理解大概意思是維護升級的時候不需要停機.
具有Ubuntu和CentOS 的啟動腳本
停止不穩定的進程(避免無限循環)
控制台檢測
提供 HTTP API
遠程控制和實時的接口API ( Nodejs 模塊,允許和PM2進程管理器交互 )
測試過Nodejs v0.11 v0.10 v0.8版本,兼容CoffeeScript,基於Linux 和MacOS.
安裝
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
如何用pm2啟動 DoraCMS:
進入源碼根目錄下執行:
pm2 start bin/www
可以使用 pm2 list 查看當前所有的進程列表。更多關於pm2的api可以查看官方文檔 https://github.com/Unitech/pm2
傳送門:http://www.html-js.cn/details/NkZMo0GDl.html