如何将节点 v8 参数和脚本参数传递给 pm2



我需要能够使用 pm2 启动下面的应用程序,但不知道如何使用 pm2 启动它。

node --expose-gc bin/www arg1 arg2 arg3

我知道--node-args但我认为仅适用于 --expose-gc。

经过一番挖掘,我发现我正在寻找的是 Linux 上的双破折号。

正常代码,

node --expose-gc bin/www arg1 arg2 arg3

使用 pm2 的相同代码

pm2 start bin/www --node-args="--expose-gc" -- arg1 arg2 arg3

您必须放入--node-args中的所有 v8 参数以及您必须在双破折号之后process.argv抓取的所有 scrips 参数。

我希望将来他们实现一些链接 --script-args="arg1 arg2 arg3"。对于那些不是Linux专家的人来说会非常好。

另一种方法是创建应用程序声明 json 文件,您可以在其中指定args密钥。查看PM2网站上的文档。

pm2.json文件示例:

{
  "apps" : [{
    "name"        : "appname",
    "script"      : "app.js",
    "args"        : ["-s", "123"],
    "node_args"   : "--harmony",
    "merge_logs"  : true,
    "cwd"         : "/this/is/a/path/to/start/script",
    "env": {
        "NODE_ENV": "production"
    }
  }]
}

并按如下方式运行它:

$ pm2 start pm2.json

您可以在 -x -- 之后添加任何自定义参数,

pm2 start app.js -x -- --prod

和节点参数作为--node-args="--harmony"

pm2 start app.js --node-args="--harmony"

pm2 start app.js --node-args="--harmony" -x -- --prod

我必须在pm2过程中暴露-gc.js所以我做了以下事情:

{
  "apps" : [
    {
      "name"        : "app",
      "script"      : "bin/www",
      "instances"   : 2,
      "exec_mode"   : "cluster",
      "watch"       : false,
      "node_args"   : "--expose-gc",
      "env"         : {"NODE_ENV": "development"}
    }
  ]
}

最新更新