地址 : https://github.com/Unitech/pm2
pm2 是一個(gè)帶有負(fù)載均衡功能的Node應(yīng)用的進(jìn)程管理器.
當(dāng)你要把你的獨(dú)立代碼利用全部的服務(wù)器上的所有CPU,并保證進(jìn)程永遠(yuǎn)都活著混稽,0秒的重載杂彭, PM2是完美的。它非常適合IaaS結(jié)構(gòu)县耽,但不要把它用于PaaS方案(隨后將開發(fā)Paas的解決方案).
備注:SaaS、PaaS和IaaS是云服務(wù)模式镣典。
SaaS 軟件即服務(wù)兔毙,例如Google的 Gmail 郵箱服務(wù).面向應(yīng)用型用戶.
PaaS 平臺(tái)即服務(wù).例如Google的GAE,面向開發(fā)型用戶
IaaS 基礎(chǔ)架構(gòu)即服務(wù),例如亞馬遜的AWS,IaaS對(duì)于不知道新推出的應(yīng)用程序/網(wǎng)站會(huì)有多成功的創(chuàng)業(yè)公司來說非常有用
請(qǐng)參考 云服務(wù)模式:SaaS兄春、PaaS和IaaS澎剥,哪一種適合你?
主要特性:
內(nèi)建負(fù)載均衡(使用Node cluster 集群模塊)
后臺(tái)運(yùn)行
0秒停機(jī)重載赶舆,我理解大概意思是維護(hù)升級(jí)的時(shí)候不需要停機(jī).
具有Ubuntu和CentOS 的啟動(dòng)腳本
停止不穩(wěn)定的進(jìn)程(避免無限循環(huán))
控制臺(tái)檢測(cè)
提供 HTTP API
遠(yuǎn)程控制和實(shí)時(shí)的接口API ( Nodejs 模塊,允許和PM2進(jìn)程管理器交互 )
檢查npm版本哑姚,最小版本v6.1.0
$ npm -v
v5.6.0
升級(jí)npm
$ npm i -g npm
1. 安裝pm2
npm install -g pm2
進(jìn)入到你的node項(xiàng)目下:
pm2 start index.js --name 'test' # index.js是我的啟動(dòng)腳本
scort@scort-Lenovo-IdeaPad-U310:~/websocket$ pm2 start index.js --name 'test'
[PM2] Starting /home/scort/websocket/index.js in fork_mode (1 instance)
[PM2] Done.
┌───────┬──────┬─────────┬───┬─────┬───────────┐
│ Name │ mode │ status │ ? │ cpu │ memory │
├───────┼──────┼─────────┼───┼─────┼───────────┤
│ index │ fork │ stopped │ 0 │ 0% │ 0 B │
│ test │ fork │ online │ 0 │ 0% │ 19.4 MB │
└───────┴──────┴─────────┴───┴─────┴───────────┘
Use `pm2 show <id|name>` to get more details about an app
監(jiān)視
$ pm2 moint
2. 把pm2加入系統(tǒng)啟動(dòng)中
$ pm2 startup ubuntu
[PM2] Init System found: systemd
-----------------------------------------------------------
PM2 detected systemd but you precised ubuntu
Please verify that your choice is indeed your init system
If you arent sure, just run : pm2 startup
-----------------------------------------------------------
Platform ubuntu
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup ubuntu
保存
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2