pm2 是Node.js下的生產環境進程管理工具,就是我們常說的進程守護工具。 可以用來在生產環境中進行自動重啟、日誌記錄、錯誤預警等等。
在我們開始之前,首先使用npm安裝 pm2 :
npm install -g pm2
PM2 文檔: https://github.com/Unitech/pm2
單線程JavaScrip的脆弱性
傳統Web服務器會為每一個HTTP請求創建線程,因而線程發生異常不會影響到整個服務器進程。 然而javascript天生是單線程的,任何未捕獲異常都會造成進程異常退出。 這也是我們需要 pm2 的關鍵原因,例如下面這個簡單的Node.js應用:
// file: index.js const http = require('http'); const server = http.createServer((req, res) => { if(req.url === '/error'){ throw 'Intended Error'; } res.end('Hello world'); }); server.listen('3000', 'localhost', () => { console.log(`Server running at http://localhost:3000/`); });
當我們訪問 http://loacalhost:3000/error
時,進程便會異常退出:
? pm2 node index.js Server running at http://localhost:3000/ /private/tmp/pm2/index.js:5 throw 'Intended Error'; ^ Intended Error
pm2基本使用
pm2 最基本的功能就是自動重啟Node.js應用,至少可以讓Node.js更穩定地運行。
? pm2 pm2 start index.js --name www [PM2] Starting index.js in fork_mode (1 instance) [PM2] Done. ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │ ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤ │ www │ 0 │ fork │ 57372 │ online │ 0 │ 0s │ 14.480 MB │ disabled │ └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘
--name
參數用來指定App name,這個App name可以通過 pm2 delete www
來刪除。
啟動後 pm2
給出了當前所有應用的狀態表。 使用 pm2 list
命令可以隨時查看上述表格。然後我們試著再次訪問 http://localhost:3000/error
, 然後查看 pm2
日誌:
? pm2 pm2 logs www [PM2] Tailing last 20 lines for [www] process www (out): Server running at http://localhost:3000/ [PM2] Streaming realtime logs for [www] process www Intended Error www Server running at http://localhost:3000/
可以看到在 Intended Error
之後 index.js
被立即重啟, 輸出 Server running at http://localhost:3000/
。 可以訪問 http://localhost:3000
來驗證服務器還活著。
日誌管理
上一節中看到 pm2 logs xxx
可以查xxx(對應App name)的日誌。 除此之外,我們還可以給日誌輸出添加時間戳:
? pm2 logs --timestamp "HH:mm:ss" www 12:31:53 www Intended Error 12:31:53 www Server running at http://localhost:3000/
在系統管理中常常會碰到巨大無比的日誌文件,其實我們可以清除太久以前的日誌。 pm2 還提供了一個模塊管理系統,通過它可以安裝 日誌輪替模塊 :
日誌存儲的默認地址是 ~/.pm2/logs/<app-name>-out-<number>.log
(標準輸出) 和 ~/.pm2/logs/<app-name>-error<number>.log
(錯誤輸出)。
pm2 install pm2-logrotate pm2 set pm2-logrotate:max_size 1K # (1KB) pm2 set pm2-logrotate:compress true # (compress logs when rotated) pm2 set pm2-logrotate:rotateInterval '*/1 * * * *' # (force rotate every minute)
參考文檔: http://pm2.keymetrics.io/docs/usage/log-management/
性能監視
pm2 也可以實時監測性能參數:
? pm2 monit ? PM2 monitoring (To go further check out https://app.keymetrics.io) ● www [ ] 0 % [0] [fork_mode] [|||| ] 26.379 MB
也可以查看某個App的詳細信息:
? pm2 pm2 describe www Describing process with id 0 - name www ┌───────────────────┬──────────────────────────────────────────┐ │ status │ online │ │ name │ www │ │ id │ 0 │ │ path │ /private/tmp/pm2/index.js │ │ args │ │ │ exec cwd │ /private/tmp/pm2 │ │ error log path │ /Users/harttle/.pm2/logs/www-error-0.log │ │ out log path │ /Users/harttle/.pm2/logs/www-out-0.log │ │ pid path │ /Users/harttle/.pm2/pids/www-0.pid │ │ mode │ fork_mode │ │ node v8 arguments │ │ │ watch & reload │ ? │ │ interpreter │ node │ │ restarts │ 3 │ │ unstable restarts │ 0 │ │ uptime │ 11m │ │ created at │ 2016-09-01T04:24:57.324Z │ └───────────────────┴──────────────────────────────────────────┘ Process configuration Probes value ┌────────────┬────────┐ │ Loop delay │ 0.63ms │ └────────────┴────────┘
發送郵件
pm2 本身不提供郵件通知支持。可以使用 https://github.com/harttle/pm2-notify 來監聽 pm2 事件。 安裝方式:
git clone https://github.com/harttle/pm2-notify cd pm2-notify && npm install # 復制一份配置文件,你的配置在config.yml中 cp config.example.yml config.yml # 復制一份郵件模板 cp template.example.md template.md # 啟動 node index.js
當任何一個pm2進程狀態發生變化時,都會發送郵件。 pm2-notify
使用 nodemailer
發送郵件, 所以 config.yml
中的SMTP等郵件相關配置都請參照 nodemailer
文檔。
Tags: localhost running private require 管理工具
文章來源: