Use of PM2 configuration files

I. Introduction

PM2 way to support application service configuration file settings, file format is supported configurations Javascript, JSON, YAML. The following focuses Javascript, JSONthese two formats;

Second, the configuration file

1. JavascriptFormat

Enter the following command to produce a configuration file template:

pm2 init

pm2 ecosystem

The generation is ecosystem.config.jsas follows:

module.exports = {
  apps : [{
    name: 'API',
    script: 'app.js',
    args: 'one two',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],

  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

You can modify the configuration content as needed and start the command:

pm2 start ecosystem.config.js

Please note that using a Javascript configuration file requires the file name to end with .config.js

2. jsonFormat

For example named config.json:

{
      "apps": [{
        "name": "test",  //名称
        "script": "server.js", //程序入口
        "cwd": "./",           //根目录
        "instances": 1,
        "error_file":"./logs/error.log",//错误输出日志
        "out_file":"./logs/out.log",  //日志
        "log_date_format":"YYYY-MM-DD HH:mm Z" //日期格式
        }]
    }

Start command:

pm2 start config.json

You can also write the order package.jsonhere. as follows:

By npm run pm2start

You can set as many JSONapplication declarations as needed .

{
  "apps": [
      {
      "name": "testOne",
      "script": " testOne/server.js",
      "instances": 1,
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "max_memory_restart": "500M"
    },
    {
      "name": "testTwo",
      "script": " testTwo/server.js",
      "instances": 1,
      "log_date_format": "YYYY-MM-DD HH:mm Z",
      "max_memory_restart": "500M"
    }
  ]
}
3. Description of common configuration items:

apps: Json structure, apps is an array, each array member corresponds to an application running in pm2;

name: Application name;

cwd: The directory where the application is located;

script: The script path of the application;

log_date_format: Specify the log date format, such as YYYY-MM-DD HH: mm: ss;

error_file: Customize the error log file of the application. Code errors can be found in this file;

out_file: Custom application log files, such as printing a large amount of standard output, will cause the pm2 log to be too large;

pid_file: Custom application pid file;

interpreter: The specified script interpreter;

interpreter_args: Parameters passed to the interpreter;

instances: The number of application startup instances, only valid in cluster mode, the default is fork;

min_uptime: The minimum running time, here is set to 60s, that is, if the application exits within 60s, pm2 will think that the program exits abnormally, at this time, the number of restart max_restarts is triggered;

max_restarts: Set the number of times the application exits abnormally and restarts, the default is 15 times (counting from 0);

autorestart : The default is true, and restart automatically when an exception occurs;

cron_restart: Start regularly to solve the problems that restart can solve;

max_memory_restart: Maximum number of memory limits, exceeding automatic restart;

watch: Whether to enable monitoring mode, the default is false. If set to true, pm2 will be automatically reloaded when the application changes. Here you can also set the file you want to monitor.

ignore_watch: Ignore the monitored folder and support regular expressions;

merge_logs: Set append log instead of new log;

exec_interpreter: The script type of the application, the default is nodejs;

exec_mode: Application startup mode, support fork and cluster mode, the default is fork;

autorestart: Enable / disable automatic restart when the application crashes or exits;

vizion: Enable / disable vizion feature (version control);

env: Environment variable, object type;

force: Default false, if true, you can start a script repeatedly. pm2 does not recommend this;

restart_delay: Delay restart time in case of abnormal restart;

Guess you like

Origin www.cnblogs.com/huiguo/p/12694542.html