如何将Node v8参数和脚本参数传递给PM2?

22

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

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

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

4个回答

30

经过一番查找,我发现我要找的是 Linux 上的双破折号。

原本的代码:

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

使用pm2相同的代码

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

所有v8参数都必须放在--node-args中,而所有脚本参数都必须在双破折号后面使用process.argv获取。

我希望将来他们能够实现类似--script-args="arg1 arg2 arg3"的东西。对于那些不是Linux专家的人来说,这将非常好。


21

另一种方法是创建应用程序声明的 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

2

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

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

并将node参数设置为--node-args="--harmony"

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

两者皆可

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


对于SailsJS,更具体的信息请参考我的这个答案:https://dev59.com/KV4b5IYBdhLWcg3wiiR4#32282896 - Nishchit

1

我需要在我的pm2 process.js中开启expose-gc,因此我必须执行以下操作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接