pm2 with systemd and passing node argument
Asked Answered
Y

3

6

I want to start node with pm2 and environment variables like --nouse-idle-notification or --max-old-space-size=2048.

However, whatever I do, it is not passing the node variables. I start my app with pm2 and a configuration file. The configuration file looks like:

{
  "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"
  }]
}

(as you can see I try to pass in the node variables in multiple ways)

I then start the app with:

pm2 restart pathtojsonfile --env production

Everything is starting up properly and I see variables like "MY_APP" in my code. However, now when I look at the process with "top" I only see:

node /opt/myapp/app.js

When I start the app with forever or manually I can see the process like:

node --max-old-space-size=2048 --nouse-idle-notification /opt/myapp/app.js

Is pm2 just not showing those node arguments or are they really not passed in? (The pm2 started process uses less memory)

Yogi answered 8/12, 2016 at 2:3 Comment(1)
Include the contents of the systemd .service file you are using with pm2.Bilodeau
H
5

By using "node_args": "--max-old-space-size=2048 --nouse-idle-notification" you did the right thing and these arguments are taken into account.

PM2 renames the process and drop the specified node argument in the process title.

Harpsichord answered 8/12, 2016 at 16:9 Comment(2)
You can do also pm2 show <app_name>to display node.js argumentHarpsichord
Yes, I did. But was really confused because the params didn't show up in the process itself, i.e. "top".Yogi
L
21

Below is extact command to start pm2 with node-args

pm2 start app.js --node-args="--max-old-space-size=4096"
Lastly answered 9/4, 2020 at 11:15 Comment(0)
H
5

By using "node_args": "--max-old-space-size=2048 --nouse-idle-notification" you did the right thing and these arguments are taken into account.

PM2 renames the process and drop the specified node argument in the process title.

Harpsichord answered 8/12, 2016 at 16:9 Comment(2)
You can do also pm2 show <app_name>to display node.js argumentHarpsichord
Yes, I did. But was really confused because the params didn't show up in the process itself, i.e. "top".Yogi
Q
0

I could not get it to work with pm2 and node_args.

When I switched to setting the environment variable

     NODE_OPTIONS="--max-old-space-size=7200"

it started working.

Quitrent answered 15/7 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.