內容轉自:PM2用法簡介html
PM2是node進程管理工具,能夠利用它來簡化不少node應用管理的繁瑣任務,如性能監控、自動重啓、負載均衡等,並且使用很是簡單。引用node
sudo npm install pm2@latest -g
python
pm2 start app.js
pm2 stop app_name|app_id
pm2 delete app_name|app_id
pm2 restart app_name|app_id
pm2 stop all
pm2 list
pm2 status
pm2 describe app_name|app_id
--watch
:監聽應用目錄源碼的變化,一旦發生變化,自動重啓。若是要精確監聽、不見聽的目錄,最好經過配置文件-i --instances
:啓用多少個實例,可用於負載均衡。若是-i 0
或者-i max
,則根據當前機器核數肯定實例數目,能夠彌補node.js缺陷--ignore-watch
:排除監聽的目錄/文件,能夠是特定的文件名,也能夠是正則。好比--ignore-watch="test node_modules "some scripts"
-n --name
:應用的名稱。查看應用信息的時候能夠用到-o --output <path>
:標準輸出日誌文件的路徑,有默認路徑-e --error <path>
:錯誤輸出日誌文件的路徑,有默認路徑--interpreter <interpreter>
:the interpreter pm2 should use for executing app (bash, python...)。好比你用的coffee script來編寫應用完整參數命令: pm2 start index.js --watch -i 2
npm
完整參數單個app配置:json
{ "name" : "node-app", //啓動app名稱 "cwd" : "/srv/node-app/current", "args" : ["--toto=heya coco", "-d", "1"], "script" : "bin/app.js", "node_args" : ["--harmony", " --max-stack-size=102400000"], "log_date_format" : "YYYY-MM-DD HH:mm Z", "error_file" : "/var/log/node-app/node-app.stderr.log", "out_file" : "log/node-app.stdout.log", "pid_file" : "pids/node-geo-api.pid", "instances" : 6, //or 0 => 'max' "min_uptime" : "200s", // 200 seconds, defaults to 1000 "max_restarts" : 10, // defaults to 15 "max_memory_restart": "1M", // 1 megabytes, e.g.: "2G", "10M", "100K", 1024 the default unit is byte. "cron_restart" : "1 0 * * *", "watch" : false, "ignore_watch" : ["[\\/\\\\]\\./", "node_modules"], "merge_logs" : true, "exec_interpreter" : "node", "exec_mode" : "fork", "autorestart" : false, // enable/disable automatic restart when an app crashes or exits "vizion" : false, // enable/disable vizion features (versioning control) // Default environment variables that will be injected in any environment and at any start "env": { "NODE_ENV": "production", "AWESOME_SERVICE_API_TOKEN": "xxx" } "env_*" : { "SPECIFIC_ENV" : true } }
完整配置文件寫法:api
{ "apps" : [{ // Application #1 "name" : "worker-app", "script" : "worker.js", "args" : ["--toto=heya coco", "-d", "1"], "watch" : true, "node_args" : "--harmony", "merge_logs" : true, "cwd" : "/this/is/a/path/to/start/script", "env": { "NODE_ENV": "development", "AWESOME_SERVICE_API_TOKEN": "xxx" }, "env_production" : { "NODE_ENV": "production" }, "env_staging" : { "NODE_ENV" : "staging", "TEST" : true } },{ // Application #2 "name" : "api-app", "script" : "api.js", "instances" : 4, "exec_mode" : "cluster_mode", "error_file" : "./examples/child-err.log", "out_file" : "./examples/child-out.log", "pid_file" : "./examples/child.pid" }] }
process.yml:bash
apps: - script : app.js instances: 4 exec_mode: cluster - script : worker.js watch : true env : NODE_ENV: development env_production: NODE_ENV: production
啓動:pm2 start process.yml
app
正式開發中分爲不一樣的環境(開發環境、測試環境、生產環境),咱們須要根據不一樣的情景來切換各類環境
pm2經過在配置文件中經過env_xx
來聲明不一樣環境的配置,而後在啓動應用時,經過--env
參數指定運行的環境負載均衡
環境配置定義,在應用中,能夠經過process.env.REMOTE_ADDR等來讀取配置中生命的變量:工具
"env": { "NODE_ENV": "production", "REMOTE_ADDR": "http://www.example.com/" }, "env_dev": { "NODE_ENV": "development", "REMOTE_ADDR": "http://wdev.example.com/" }, "env_test": { "NODE_ENV": "test", "REMOTE_ADDR": "http://wtest.example.com/" }
啓動指定的環境:pm2 start app.js --env development
pm2 start app.js -i 3 # 開啓三個進程 pm2 start app.js -i max # 根據機器CPU核數,開啓對應數目的進程
安裝最新的:npm install pm2@latest -g
而後在內存中更新:pm2 update