1. 程式人生 > >pm2 官方文檔 學習筆記

pm2 官方文檔 學習筆記

ORC 3.1 iou rev 守護 itl 監視 如何 gist

一、安裝


1、安裝


npm install pm2 -g

2、更新


npm install pm2 -g && pm2 update

pm2 update 是為了刷新 PM2 的守護進程

二、使用 js 配置文件啟動


1、生成配置文件


pm2 ecosystem

會自動生成 ecosystem.config.js 文件 (下文的 "五、配置文件實例" 會詳細說到如何配置)

2、啟動配置文件


pm2 start /path/ecosystem.config.js 
pm2 start /path/ecosystem.config.js -i max
// PM2 將自動檢測可用 CPU 的數量並運行盡可能多的進程

三、管理 PM2 進程


1、常規


pm2 start

pm2 restart

pm2 reload

pm2 ls

pm2 stop

pm2 delete

restart vs reload

restart:pm2 同時殺死並重啟所有進程。短時間內服務不可用

reload:pm2 逐個重啟所有進程,但始終保持至少一個進程在運行

所以推薦使用 reload

2、操作多個進程


寫法一:

pm2 restart / reload / stop / delete app1 app2 app3

寫法二:

pm2 restart / reload / stop / delete all

node.js 中如何判斷當前程序執行的是哪個進程?

使用 NODE_APP_INSTANCE 環境變量

if(process.env.NODE_APP_INSTANCE === 0){
    //todo
}

3、保存 / 恢復進程 list


# save your list in hard disk memory
pm2 save

# resurrect your list previously saved
pm2 resurrect

4、監視進程


pm2 monit

5、使主機重啟後可以恢復之前的進程


pm2 startup
//在 CLI 中復制並粘貼此命令的輸出以設置啟動掛鉤

pm2 unstartup

四、日誌


# all apps logs
pm2 logs

# only app logs
pm2 logs app

五、配置文件實例



module.exports = {
  /**
   * Application configuration section
   * http://pm2.keymetrics.io/docs/usage/application-declaration/
   */
  apps: [

    // First application
    {
      name: ‘UB‘,
      script: ‘./bin/www‘,
      instances: "max",
      env: {
        NODE_ENV: ‘development‘
      },
      env_testing: {
        NODE_ENV: ‘testing‘
      },
      env_production: {
        NODE_ENV: ‘production‘
      },
    //   log combines output and error, disabled by default
    //   "log": "./logs/combined.log", 
      "error_file": "./logs/pm2_UB_err.log",
      "out_file": "./logs/pm2_UB_out.log",
    //   In cluster mode, each cluster has his own log files. You can use the merge options to gather all logs into a single file
      "merge_logs": true, 
    //   "log_type":"json"
      "log_date_format": "YYYY-MM-DD HH:mm:ss Z"
    },

    // Second application
    {
      name: ‘UB_schedule‘,
      script: ‘./campaign_schedule.js‘,
      "error_file": "./logs/pm2_UB_schedule_err.log",
      "out_file": "./logs/pm2_UB_schedule_out.log",
      "merge_logs": true,
      "log_date_format": "YYYY-MM-DD HH:mm:ss Z"
    }
  ],

  /**
   * Deployment section
   * http://pm2.keymetrics.io/docs/usage/deployment/
   */
  deploy: { 

    testing: {
      user: ‘universebackend‘,
      host: ‘xxx.xx.93.179‘,
      ssh_options: "StrictHostKeyChecking=no",
      ref: ‘origin/backend-api‘,
      repo: ‘[email protected]:production-team/universal_backend.git‘,
      path: ‘/home/universebackend/universalapi‘,
      
      ‘post-deploy‘: ‘npm install --registry=https://registry.npm.taobao.org && pm2 startOrRestart ecosystem.config.js --env test‘,
    }

    production: {
    //   SSH key path, default to $HOME/.ssh
    //   key: "/path/to/some.pem",
      // SSH user
      user: ‘universebackend‘,
      // SSH host
      host: [‘xxx.xx.103.209‘, ‘xxx.xx.98.216‘, ‘xxx.xx.61.173‘],
    //   SSH options with no command-line flag, see ‘man ssh‘
    //   can be either a single string or an array of strings
      ssh_options: "StrictHostKeyChecking=no",
    //   GIT remote/branch
      ref: ‘origin/backend-api‘,
    //   GIT remote
      repo: ‘[email protected]:production-team/universal_backend.git‘,
    //   path in the server
      path: ‘/home/universebackend/givenchy‘,

    //   Pre-setup command or path to a script on your local machine
      pre-setup: "yum install git ; ls -la",
    //   Post-setup commands or path to a script on the host machine
    //   eg: placing configurations in the shared dir etc
      post-setup: "ls -la",

    //    pre-deploy action
      ‘pre-deploy-local‘: "echo ‘This is a local executed command‘",
    //   post-deploy action
      ‘post-deploy‘: ‘npm install --registry=https://registry.npm.taobao.org && pm2 startOrRestart ecosystem.config.js --env production‘,
    }

  }

};

這裏定義了 development | testing | production 三個環境變量

如何指定環境變量:

//啟動
pm2 start ecosystem.config.js --env production

//更新
pm2 restart ecosystem.config.js --env production --update-env

nodejs中如何使用環境變量:

process.env.[環境變量]

if (process.env.NODE_ENV == "production") {

} else if (process.env.NODE_ENV == "testing") {
   
} else if (process.env.NODE_ENV == "development") {
   
} else {
    
}

3、deploy 部署

(1)跟要部署的遠程服務器建立好 ssh 連接(使用 密鑰認證 方式)

詳見我的另一篇《 SSH 學習筆記 》

(2)先 setup

pm2 deploy ecosystem.config.js production setup

實質:執行了 git clone 的操作

(3)再 deploy

pm2 deploy ecosystem.config.js production

實質:執行了 git fetch 的操作


(4)其他操作

# Update remote version
pm2 deploy production update

# Revert to -1 deployment
pm2 deploy production revert 1

# execute a command on remote servers
pm2 deploy production exec "pm2 reload all"

(5)強制部署

如果你在本地修改了 ecosystem.config.js卻沒有 push 到 github 上,這個時候 deploy 會報錯:

--> Deploying to dev environment
--> on host 192.168.1.XX

  push your changes before deploying

Deploy failed

這時就可以使用到強制部署

pm2 deploy ecosystem.config.js production --force 

註:--force 只對 ecosystem.config.js 配置中的 “deploy” 部分有效,“apps” 部分依然是以 github 為準


參考資料:

1.【 pm官方文檔 】https://pm2.io/doc/en/runtime/guide/installation/#install-pm2

pm2 官方文檔 學習筆記