PM2是Node.js應用程序的生產流程管理器,內置負載均衡。它可以幫助您保持Node應用程序永久活動,重起這些node應用程序也不需要停機,並簡化常見的系統管理任務。
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
在生產模式下啟動應用程序非常簡單
$ pm2 start app.js
安裝 PM2
$ npm install pm2 -g
npm is a builtin CLI when you install Node.js - Installing Node.js with NVM
啟動一個應用程序
$ pm2 start app.js
Your app is now daemonized, monitored and kept alive forever.
更新 PM2
# 安裝最新的pm2
$ npm install pm2 -g
# Save process list, exit old PM2 & restore all processes
$ pm2 update
PM2 updates are seamless
主要功能
常用命令一覽
# 普通General $ npm install pm2 -g # 安裝 PM2 $ pm2 start app.js # 啟動,守護進程,自動重啟應用程序 Start, Daemonize and auto-restart application (Node) $ pm2 start app.py # 啟動,守護進程,自動重啟python應用程序 Start, Daemonize and auto-restart application (Python) $ pm2 start npm -- start # 啟動,守護進程,自動重啟node應用程序 Start, Daemonize and auto-restart Node application # 集群模式 (只支持node進程) Cluster Mode (Node.js only) $ pm2 start app.js -i 4 # 在集群模式下,啟動4個應用程序實例 Start 4 instances of application in cluster mode # 同時,將網絡請求,負載均衡到每個應用實例 it will load balance network queries to each app $ pm2 reload all # 0秒重啟所有應用 Zero Second Downtime Reload $ pm2 scale [app-name] 10 # 將應用進程調整到10 Scale Cluster app to 10 process # 進程監控 Scale Cluster app to 10 process $ pm2 list # 列出所有用PM2啟動的進程 List all processes started with PM2 $ pm2 monit # 顯示每個應用占用的cpu和內存 Display memory and cpu usage of each app $ pm2 show [app-name] # 顯示某個進程的所有信息 Show all informations about application # 日志管理 Log management $ pm2 logs # 顯示所有應用的日志 Display logs of all apps $ pm2 logs [app-name] # 顯示某個應用的日志 Display logs for a specific app $ pm2 logs --json # json化日志 Logs in JSON format $ pm2 flush $ pm2 reloadLogs # 進程狀態管理 Process State Management $ pm2 start app.js --name="api" # 啟動一個應用並命名為api。 Start application and name it "api" $ pm2 start app.js -- -a 34 # 啟動一個應用,並傳遞“-a 34”的參數。 Start app and pass option "-a 34" as argument $ pm2 start app.js --watch # 重啟一個應用,當文件改變的時候。Restart application on file change $ pm2 start script.sh # 啟動一個bash腳本。Start bash script $ pm2 start app.json # 啟動在app.json中聲明的所有應用。Start all applications declared in app.json $ pm2 reset [app-name] # 重置所有計數器。Reset all counters $ pm2 stop all # 停止所有應用。Stop all apps $ pm2 stop 0 # 停止id 為0的應用。Stop process with id 0 $ pm2 restart all # 重啟所有應用。Restart all apps $ pm2 gracefulReload all # 在集群模式下,平穩的重加載所有應用。Graceful reload all apps in cluster mode $ pm2 delete all # 殺掉所有應用。Kill and delete all apps $ pm2 delete 0 # 殺掉id為0的進程。Delete app with id 0 # 啟動/引導管理 Startup/Boot management $ pm2 startup # 檢測init系統,在啟動時生成和配置pm2。Detect init system, generate and configure pm2 boot on startup $ pm2 save # 保存當前進程列表。Save current process list $ pm2 resurrect # 恢復以前保存的進程。Restore previously save processes $ pm2 unstartup # 停用和刪除啟動系統。Disable and remove startup system $ pm2 update # 保存進程,終止PM2並恢復進程。Save processes, kill PM2 and restore processes $ pm2 generate # 生成樣本json配置文件。Generate a sample json configuration file # 部署 Deployment $ pm2 deploy app.json prod setup # 設置“生產環境”遠程服務器。 Setup "prod" remote server $ pm2 deploy app.json prod # 更新“生產環境”遠程服務器。 Update "prod" remote server $ pm2 deploy app.json prod revert 2 # 將“生產環境”遠程服務器恢復2。Revert "prod" remote server by 2 # 模塊系統 Module system $ pm2 module:generate [name] # 生成名稱為[name]的示例模塊。Generate sample module with name [name] $ pm2 install pm2-logrotate # 安裝模塊(這里是日志循環系統)。Install module (here a log rotation system) $ pm2 uninstall pm2-logrotate # 卸載模塊。Uninstall module $ pm2 publish # 增量版本,git push和npm發布。Increment version, git push and npm publish
進程管理
一旦應用程序啟動,您可以輕松地列出和管理它們:
列出正在運行的應用程序
$ pm2 list
很簡單的管理你的應用進程
$ pm2 stop <app_name|id|'all'|json_conf>
$ pm2 restart <app_name|id|'all'|json_conf>
$ pm2 delete <app_name|id|'all'|json_conf>
要確保它在json_conf使用中重新評估環境變量 To make sure it re-evaluates enviroment variables in your json_conf
use
$ pm2 restart <json_conf>
要了解特定流程的更多詳細信息
$ pm2 describe <id|app_name>
Load Balancing & Zero second Downtime Reload
When an application is started with the -i option, the Cluster Mode is enabled.
The Cluster Mode start and automatically load balance HTTP/TCP/UDP between each instance. This allows to increase overall performance depending to the number of CPUs availabe.
Seamlessly supported by all major Node.js frameworks and any Node.js applications without any code change:
Main commands:
主要命令:
$ pm2 start app.js -i max # 啟用負載平衡,啟動'max'實例(cpu nb)。Enable load-balancer and start 'max' instances (cpu nb)
$ pm2 reload all # 0秒重啟。Zero second dowtime reload
$ pm2 scale <app_name> <instance_number> # 增加/減少進程號。Increase / Decrease process number
More informations about how PM2 make clustering easy
CPU/內存 監控
監控所有啟動的進程:Monitoring all processes launched:
$ pm2 monit
日志設施 Log facilities
實時顯示指定進程或所有進程的日志。提供標准,原始,JSON和格式輸出。
$ pm2 logs ['all'|app_name|app_id] [--json] [--format] [--raw]`
例子:
$ pm2 logs APP-NAME # Display APP-NAME logs
$ pm2 logs --json # JSON output
$ pm2 logs --format # Formated output
$ pm2 flush # Flush all logs
$ pm2 reloadLogs # Reload all logs
生成啟動腳本 Startup script generation
PM2可以生成和配置啟動腳本,以在每次服務器重新啟動時保持PM2和您的進程活動。 支持init系統,如:systemd(Ubuntu 16,CentOS,Arch),upstart(Ubuntu 14/12),launchd(MacOSx,Darwin),rc.d(FreeBSD)。
# 自動檢測init系統+在服務器啟動時生成和設置PM2引導。 Auto detect init system + generate and setup PM2 boot at server startup
$ pm2 startup
# 手動指定啟動系統. Manually specify the startup system
# 可以是:systemd,upstart,launchd,rcd。
$ pm2 startup [platform]
# 在服務器啟動時禁用和刪除PM2引導。Disable and remove PM2 boot at server startup
$ pm2 unstartup
在重新啟動時保存/凍結進程列表:To save/freeze a process list on reboot:
$ pm2 save
模塊系統 Module system
PM2嵌入了一個簡單而強大的模塊系統。安裝模塊很簡單:
$ pm2 install <module_name>
下面是一些PM2兼容模塊(由PM2管理的獨立Node.js應用程序):Here are some PM2 compatible modules (standalone Node.js applications managed by PM2):
pm2-logrotate 自動輪換PM2的日志和管理的應用程序 (auto rotate logs of PM2 and applications managed)
pm2-webshell 在瀏覽器中展示一個功能完備的終端(expose a fully capable terminal in browsers)
pm2-server-monit 監視您的服務器運行狀況(monitor your server health)
關鍵指標監控 Keymetrics monitoring
如果您使用PM2管理NodeJS應用程序,Keymetrics可以輕松地跨服務器監控和管理應用程序。隨意試試:
Discover the monitoring dashboard for PM2
感謝,我們希望你喜歡PM2!
translate by 青菜葉子(zhentaoo)