JSON应用声明

可以在 processes.json定义应用参数:

{
  "apps" : [{
    "name"        : "echo",
    "script"      : "examples/args.js",
    "args"        : "['--toto=heya coco', '-d', '1']",
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "ignoreWatch" : ["[\\/\\\\]\\./", "node_modules"],
    "watch"       : true,
    "node_args"   : "--harmony",
    "cwd"         : "/this/is/a/path/to/start/script",
    "env": {
        "NODE_ENV": "production",
        "AWESOME_SERVICE_API_TOKEN": "xxx"
    }
  },{
    "name"       : "api",
    "script"     : "./examples/child.js",
    "instances"  : "4",
    "log_date_format"  : "YYYY-MM-DD",
    "log_file"   : "./examples/child.log",
    "error_file" : "./examples/child-err.log",
    "out_file"   : "./examples/child-out.log",
    "pid_file"   : "./examples/child.pid",
    "exec_mode"  : "cluster_mode",
    "port"       : 9005
  },{
    "name"       : "auto-kill",
    "script"     : "./examples/killfast.js",
    "min_uptime" : "100",
    "exec_mode"  : "fork_mode"
  }]
}

然后运行:

$ pm2 start processes.json
$ pm2 stop processes.json
$ pm2 delete processes.json
$ pm2 restart processes.json

有关JSON的应用程序声明的几点说明:

  • All command line options passed when using the JSON app declaration will be dropped i.e.
$ cat node-app-1.json

{
  "name" : "node-app-1",
  "script" : "app.js",
  "cwd" : "/srv/node-app-1/current"
}
  • JSON app declarations are additive. Continuing from above:

    $ pm2 start node-app-2.json
    $ ps aux | grep node-app
    root  14735  5.8  1.1  752476  83932 ? Sl 00:08 0:00 pm2: node-app-1
    root  24271  0.0  0.3  696428  24208 ? Sl 17:36 0:00 pm2: node-app-2
    

    Note that if you execute pm2 start node-app-2 again, it will spawn an additional instance node-app-2.

  • cwd: your JSON declaration does not need to reside with your script. If you wish to maintain the JSON(s) in a location other than your script (say, /etc/pm2/conf.d/node-app.json) you will need to use the cwd feature. (Note, this is especially helpful for capistrano style directory structures that use symlinks.) Files can be either relative to the cwd directory, or absolute (example below.)

  • The following are valid options for JSON app declarations:

    [{
    "name"             : "node-app",
    "cwd"              : "/srv/node-app/current",
    "args"             : "['--toto=heya coco', '-d', '1']",
    "script"           : "bin/app.js",
    "node_args"        : ["--harmony", " --max-stack-size=102400000"],
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "error_file"       : "/var/log/node-app/node-app.stderr.log",
    "out_file"         : "log/node-app.stdout.log",
    "pid_file"         : "pids/node-geo-api.pid",
    "instances"        : 6, //or 0 => 'max'
    "min_uptime"       : "200s", // 200 seconds, defaults to 1000
    "max_restarts"     : 10, // defaults to 15
    "max_memory_restart": "1M", // 1 megabytes, e.g.: "2G", "10M", "100K", 1024... the default unit is byte.
    "cron_restart"     : "1 0 * * *",
    "watch"            : false,
    "ignoreWatch"      : ["[\\/\\\\]\\./", "node_modules"],
    "merge_logs"       : true,
    "exec_interpreter" : "node",
    "exec_mode"        : "fork",
    "env": {
      "NODE_ENV": "production",
      "AWESOME_SERVICE_API_TOKEN": "xxx"
    }
    }]