使用systemd和传递Node参数的PM2

6

我想使用pm2和环境变量比如--nouse-idle-notification 或者 --max-old-space-size=2048来启动node。

但是,无论我怎么做,都不能将node变量传递给它。我使用pm2和配置文件启动我的应用程序。配置文件看起来像这样:

{
  "apps" : [{
    "env": {
      "NODE_PATH": "/usr/bin/node",
      "interpreter_args": "--max-old-space-size=2048 --nouse-idle-notification"
    },
    "env_development": {
      "NODE_ENV": "development"
    },
    "env_production" : {
       "NODE_ENV": "production",
       "APP_TYPE": "web"
    },
    "exec_mode"   : "fork",
    "name"        : "MyApp",
    "script"      : "/opt/myapp/app.js",
    "watch"       : false,
    "out_file"    : "/var/log/app.log",
    "error_file"  : "/var/log/app.log",
    "combine_logs": true,
    "node_args": "--max-old-space-size=2048 --nouse-idle-notification",
    "args": "--max-old-space-size=2048 --nouse-idle-notification"
  }]
}

正如您所看到的,我尝试以多种方式传递节点变量。

然后我使用以下命令启动应用程序:

pm2 restart pathtojsonfile --env production

一切都正常启动,我的代码中出现了“MY_APP”等变量。但是,当我使用“top”查看进程时,我只看到:

node /opt/myapp/app.js

当我使用forever或手动启动应用程序时,可以看到进程如下所示:
node --max-old-space-size=2048 --nouse-idle-notification /opt/myapp/app.js

pm2是否只是没有显示这些node参数,还是确实没有传递进去?(pm2启动的进程使用更少的内存)


请包含您在使用pm2时的systemd .service文件的内容。 - Mark Stosberg
2个回答

19

以下是使用node-args启动pm2的精确命令

pm2 start app.js --node-args="--max-old-space-size=4096"

5

通过使用 "node_args": "--max-old-space-size=2048 --nouse-idle-notification",您做得很对,这些参数已被考虑在内。

PM2会重命名进程并在进程标题中删除指定的节点参数。


1
您也可以使用 pm2 show <app_name> 命令来显示 Node.js 参数。 - Unitech
是的,我做了。但是我真的很困惑,因为参数在进程本身中没有显示出来,即“top”。 - Nitai

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