nodejs pm2教程(轉載)

 

1、簡介

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

2、安裝

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

  1. cd oneinstack/src
  2. wget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz
  3. tar xzf node-v4.2.4-linux-x64.tar.gz
  4. cp node-v4.2.4-linux-x64/bin/node /usr/local/bin/
  5. cp -R node-v4.2.4-linux-x64/lib/node_modules /usr/local/lib/
  6. ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
  7. npm install pm2@latest -g #安裝最新版本pm2模塊

PS: 若是你的主機沒法鏈接公網,先找到能連公網的主機安裝上面的方法安裝pm2,而後拷貝到你要安裝的主機。拷貝以下目錄:html

  1. /usr/local/bin/node
  2. /usr/local/lib/node_modules

再建立相關軟鏈接java

3、PM2經常使用命令

假設你如今已經寫好了一個app.js的文件,須要啓動,你可使用pm2進行管理node

1. 啓動

  1. # pm2 start app.js
  2. # pm2 start app.js --name my-api #my-api爲PM2進程名稱
  3. # pm2 start app.js -i 0 #根據CPU核數啓動進程個數
  4. # pm2 start app.js --watch #實時監控app.js的方式啓動,當app.js文件有變更時,pm2會自動reload

2. 查看進程

  1. # pm2 list
  2. # pm2 show 0 或者 # pm2 info 0 #查看進程詳細信息,0爲PM2進程id

3. 監控

  1. # pm2 monit

4. 中止

  1. # pm2 stop all #中止PM2列表中全部的進程
  2. # pm2 stop 0 #中止PM2列表中進程爲0的進程

5. 重載

  1. # pm2 reload all #重載PM2列表中全部的進程
  2. # pm2 reload 0 #重載PM2列表中進程爲0的進程

6. 重啓

  1. # pm2 restart all #重啓PM2列表中全部的進程
  2. # pm2 restart 0 #重啓PM2列表中進程爲0的進程

7. 刪除PM2進程

  1. # pm2 delete 0 #刪除PM2列表中進程爲0的進程
  2. # pm2 delete all #刪除PM2列表中全部的進程

8. 日誌操做

  1. # pm2 logs [--raw] #Display all processes logs in streaming
  2. # pm2 flush #Empty all log file
  3. # pm2 reloadLogs #Reload all logs

9. 升級PM2

  1. # npm install pm2@lastest -g #安裝最新的PM2版本
  2. # pm2 updatePM2 #升級pm2

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

  1. # pm2 --help

4、PM2目錄結構

默認的目錄是:當前用於的家目錄下的.pm2目錄(此目錄能夠自定義,請參考:5、自定義啓動文件),詳細信息以下:python

  1. $HOME/.pm2 #will contain all PM2 related files
  2. $HOME/.pm2/logs #will contain all applications logs
  3. $HOME/.pm2/pids #will contain all applications pids
  4. $HOME/.pm2/pm2.log #PM2 logs
  5. $HOME/.pm2/pm2.pid #PM2 pid
  6. $HOME/.pm2/rpc.sock #Socket file for remote commands
  7. $HOME/.pm2/pub.sock #Socket file for publishable events
  8. $HOME/.pm2/conf.js #PM2 Configuration

5、自定義啓動文件

建立一個test.json的示例文件,格式以下:linux

  1. {
  2. "apps":
  3. {
  4. "name": "test",
  5. "cwd": "/data/wwwroot/nodejs",
  6. "script": "./test.sh",
  7. "exec_interpreter": "bash",
  8. "min_uptime": "60s",
  9. "max_restarts": 30,
  10. "exec_mode" : "cluster_mode",
  11. "error_file" : "./test-err.log",
  12. "out_file": "./test-out.log",
  13. "pid_file": "./test.pid"
  14. "watch": false
  15. }
  16. }

說明:shell

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

name:應用程序的名稱json

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會自動重載。這裏也能夠設置你要監控的文件。

詳細參數列表:見附件八

6、實例

已上面的test.json爲例

  1. # cat > /data/wwwroot/nodejs/test.sh << EOF
  2. #!/bin/bash
  3. while :
  4. do
  5. echo "Test" >> 1.log
  6. sleep 5
  7. done
  8. EOF
  1. # chmod +x test.sh #添加執行權限
  2. # pm2 start test.json #啓動,以下圖:
  1. # pm2 list #查看pm2進程,以下圖:

7、備註

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

8、附件

Field Type Example Description
name string "myAPI" name your app will have in PM2
script string "bin/app.js" path of your app
args list ["--enable-logs", "-n", "15"] arguments given to your app when it is launched
node_args list ["--harmony", "--max-stack-size=1024"] arguments given to node when it is launched
cwd string "/var/www/app/prod" the directory from which your app will be launched
exec_mode string "cluster" "fork" mode is used by default, "cluster" mode can be configured with instances field
instances number 4 number 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_interpreter string "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_format string "YYYY-MM-DD HH:mm Z" format in which timestamps will be displayed in the logs
error_file string "/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 <app id>
out_file string "/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 <app id>
pid_file string "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 <app id>
merge_logs boolean false defaults to false. if true, it will merge logs from all instances of the same app into the same file
cron_restart string "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
watch boolean true enables 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_watch list ["[\/\\]\./", "node_modules"] list of regex to ignore some file or folder names by the watch feature
min_uptime number 1000 min 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_restarts number 10 number 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_restart string "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...
env object {"NODE_ENV": "production", "ID": "42"} env variables which will appear in your app
autorestart boolean false true by default. if false, PM2 will not restart your app if it crashes or ends peacefully
vizion boolean false true by default. if false, PM2 will start without vizion features (versioning control metadatas)
post_update list ["npm install", "echo launching the app"] a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard
force boolean true defaults to false. if true, you can start the same script several times which is usually not allowed by PM2
next_gen_js boolean true defaults to false. if true, PM2 will launch your app using embedded BabelJS features which means you can run ES6/ES7 javascript code
restart_delay number 4000 time to wait before restarting a crashed app (in milliseconds). defaults to 0.
相關文章
相關標籤/搜索