nodejs pm2教程

一、簡介

pm2是一個帶有負載均衡功能的應用進程管理器,類似有Supervisor,forever。

二、安裝

Linux Binaries下載地址:https://nodejs.org/dist

cdoneinstack/src

wget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz

tar?xzf?node-v4.2.4-linux-x64.tar.gz

cp?node-v4.2.4-linux-x64/bin/node?/usr/local/bin/

cp?-R?node-v4.2.4-linux-x64/lib/node_modules?/usr/local/lib/

ln?-s?/usr/local/lib/node_modules/npm/bin/npm-cli.js?/usr/local/bin/npm

npm?install?pm2@latest?-g #安裝最新版本pm2模塊

PS: 如果你的主機無法連接公網,先找到能連公網的主機安裝上面的方法安裝pm2,然后拷貝到你要安裝的主機。拷貝如下目錄:

/usr/local/bin/node

/usr/local/lib/node_modules

再創建相關軟連接

三、PM2常用命令

假設你現在已經寫好了一個app.js的文件,需要啟動,你可以使用pm2進行管理

1. 啟動

# pm2?start?app.js

# pm2?start?app.js?--name?my-api#my-api為PM2進程名稱

# pm2?start?app.js?-i?0#根據CPU核數啟動進程個數

# pm2?start?app.js?--watch#實時監控app.js的方式啟動,當app.js文件有變動時,pm2會自動reload

2. 查看進程

# pm2list

# pm2?show?0?或者#?pm2?info?0??#查看進程詳細信息,0為PM2進程id

3. 監控

#?pm2?monit

4. 停止

#?pm2?stop?all??#停止PM2列表中所有的進程

#?pm2?stop?0????#停止PM2列表中進程為0的進程

5. 重載

#?pm2?reload?all????#重載PM2列表中所有的進程

#?pm2?reload?0?????#重載PM2列表中進程為0的進程

6. 重啟

#?pm2?restart?all?????#重啟PM2列表中所有的進程

#?pm2?restart?0??????#重啟PM2列表中進程為0的進程

7. 刪除PM2進程

#?pm2?delete?0?????#刪除PM2列表中進程為0的進程

#?pm2?delete?all???#刪除PM2列表中所有的進程

8. 日志操作

#?pm2?logs?[--raw]???#Display?all?processes?logs?in?streaming

#?pm2?flush??????????????#Empty?all?log?file

#?pm2?reloadLogs????#Reload?all?logs

9. 升級PM2

#?npm?install?pm2@lastest?-g???#安裝最新的PM2版本

#?pm2?updatePM2????????????????????#升級pm2

10. 更多命令參數請查看幫助

#?pm2?--help

四、PM2目錄結構

默認的目錄是:當前用于的家目錄下的.pm2目錄(此目錄可以自定義,請參考:五、自定義啟動文件),詳細信息如下:

$HOME/.pm2#will?contain?all?PM2?related?files

$HOME/.pm2/logs#will?contain?all?applications?logs

$HOME/.pm2/pids#will?contain?all?applications?pids

$HOME/.pm2/pm2.log#PM2?logs

$HOME/.pm2/pm2.pid#PM2?pid

$HOME/.pm2/rpc.sock#Socket?file?for?remote?commands

$HOME/.pm2/pub.sock#Socket?file?for?publishable?events

$HOME/.pm2/conf.js#PM2?Configuration

五、自定義啟動文件

創建一個test.json的示例文件,格式如下:

{

"apps":

{

"name":?"test",

"cwd":?"/data/wwwroot/nodejs",

"script":?"./test.sh",

"exec_interpreter":?"bash",

"min_uptime":?"60s",

"max_restarts":?30,

"exec_mode"?:?"cluster_mode",

"error_file"?:?"./test-err.log",

"out_file":?"./test-out.log",

"pid_file":?"./test.pid"

"watch":?false

}

}

說明:

apps:json結構,apps是一個數組,每一個數組成員就是對應一個pm2中運行的應用

name:應用程序的名稱

cwd:應用程序所在的目錄

script:應用程序的腳本路徑

exec_interpreter:應用程序的腳本類型,這里使用的shell,默認是nodejs

min_uptime:最小運行時間,這里設置的是60s即如果應用程序在60s內退出,pm2會認為程序異常退出,此時觸發重啟max_restarts設置數量

max_restarts:設置應用程序異常退出重啟的次數,默認15次(從0開始計數)

exec_mode:應用程序啟動模式,這里設置的是cluster_mode(集群),默認是fork

error_file:自定義應用程序的錯誤日志文件

out_file:自定義應用程序日志文件

pid_file:自定義應用程序的pid文件

watch:是否啟用監控模式,默認是false。如果設置成true,當應用程序變動時,pm2會自動重載。這里也可以設置你要監控的文件。

詳細參數列表:見附件八

六、實例

已上面的test.json為例

#?cat?>?/data/wwwroot/nodejs/test.sh?<<?EOF

#!/bin/bash

while:

do

echo?"Test"?>>?1.log

sleep?5

done

EOF

#?chmod?+x?test.sh??????#添加執行權限

#?pm2?start?test.json????#啟動,如下圖:

#?pm2?list????#查看pm2進程,如下圖:

七、備注

其他可參數見官網:http://pm2.keymetrics.io

八、附件

FieldTypeExampleDescription

namestring"myAPI"name your app will have in PM2

scriptstring"bin/app.js"path of your app

argslist["--enable-logs", "-n", "15"]arguments given to your app when it is launched

node_argslist["--harmony", "--max-stack-size=1024"]arguments given to node when it is launched

cwdstring"/var/www/app/prod"the directory from which your app will be launched

exec_modestring"cluster""fork" mode is used by default, "cluster" mode can be configured with instances field

instancesnumber4number of instances for your clustered app, 0 means as much instances as you have CPU cores. a negative value means CPU cores - value (e.g -1 on a 4 cores machine will spawn 3 instances)

exec_interpreterstring"node"defaults to "node". can be "python", "ruby", "bash" or whatever interpreter you wish to use. "none" will execute your app as a binary executable

log_date_formatstring"YYYY-MM-DD HH:mm Z"format in which timestamps will be displayed in the logs

error_filestring"/var/log/node-app/node-app.stderr.log"path to the specified error log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc

out_filestring"/var/log/node-app/node-app.stdout.log"path to the specified output log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc

pid_filestring"pids/node-geo-api.pid"path to the specified pid file. PM2 generates one by default if not specified and you can find it by typing pm2 desc

merge_logsbooleanfalsedefaults to false. if true, it will merge logs from all instances of the same app into the same file

cron_restartstring"1 0 * * *"a cron pattern to restart your app. only works in "cluster" mode for now. soon to be avaible in "fork" mode as well

watchbooleantrueenables the watch feature, defaults to "false". if true, it will restart your app everytime a file change is detected on the folder or subfolder of your app.

ignore_watchlist["[\/\\]\./", "node_modules"]list of regex to ignore some file or folder names by the watch feature

min_uptimenumber1000min uptime of the app to be considered started (i.e. if the app crashes in this time frame, the app will only be restarted the number set in max_restarts (default 15), after that it's errored)

max_restartsnumber10number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being

max_memory_restartstring"150M"your app will be restarted by PM2 if it exceeds the amount of memory specified. human-friendly format : it can be "10M", "100K", "2G" and so on...

envobject{"NODE_ENV": "production", "ID": "42"}env variables which will appear in your app

autorestartbooleanfalsetrue by default. if false, PM2 will not restart your app if it crashes or ends peacefully

vizionbooleanfalsetrue by default. if false, PM2 will start without vizion features (versioning control metadatas)

post_updatelist["npm install", "echo launching the app"]a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard

forcebooleantruedefaults to false. if true, you can start the same script several times which is usually not allowed by PM2

next_gen_jsbooleantruedefaults to false. if true, PM2 will launch your app using embedded BabelJS features which means you can run ES6/ES7 javascript code

restart_delaynumber4000time to wait before restarting a crashed app (in milliseconds). defaults to 0.


原文地址:https://blog.linuxeye.cn/435.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,572評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,071評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,409評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,569評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,360評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,895評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,979評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,123評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,643評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,559評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,742評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,250評論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,981評論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,363評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,622評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,354評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,707評論 2 370

推薦閱讀更多精彩內容