pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

PM2 doesn't remove environment variables even with --update-env

Open Kmaschta opened this issue 6 years ago • 11 comments

What's going wrong?

At process restart or reload, PM2 keeps the environment variables that are removed in the process file.

How could we reproduce this issue?

With the following JS file:

const http = require('http');

console.log('MY_ENV_VAR', process.env.MY_ENV_VAR);

const server= http.createServer((req, res) => res.end());
server.listen(8000);
console.log('Listening on port 8000');

And the following process file:

{
    "apps": [{
        "name": "test",
        "script": "./index.js",
        "env": {
            "MY_ENV_VAR": "is set"
        }
    }]
}

Start the process:

$ pm2 start processes.json
$ pm2 logs
0|test     | MY_ENV_VAR is set
0|test     | Listening on port 8000

Now remove the environment variable and restart --update-env:

{
    "apps": [{
        "name": "test",
        "script": "./index.js"
    }]
}
$ pm2 restart processes.json --update-env
$ pm2 logs
0|test     | MY_ENV_VAR is set
0|test     | Listening on port 8000
0|test     | MY_ENV_VAR is set
0|test     | Listening on port 8000

Supporting information

Please run the following command (available on PM2 >= 2.6)

$ pm2 report
===============================================================================
--- PM2 REPORT (Thu Feb 22 2018 10:28:45 GMT+0100 (CET)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 2.9.3
node version         : 8.9.4
node path            : /home/kmaschta/.nvm/versions/node/v8.9.4/bin/pm2
argv                 : /home/kmaschta/.nvm/versions/node/v8.9.4/bin/node,/home/kmaschta/.nvm/versions/node/v8.9.4/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : kmaschta
uid                  : 1000
gid                  : 1000
uptime               : 2min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.9.3
node version         : 8.9.4
node path            : /home/kmaschta/.nvm/versions/node/v8.9.4/bin/pm2
argv                 : /home/kmaschta/.nvm/versions/node/v8.9.4/bin/node,/home/kmaschta/.nvm/versions/node/v8.9.4/bin/pm2,report
argv0                : node
user                 : kmaschta
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
cpus nb              : 4
freemem              : 6169698304
totalmem             : 16521183232
home                 : /home/kmaschta
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ test     │ 0  │ fork │ 17410 │ online │ 1       │ 37s    │ 0%  │ 32.8 MB   │ kmaschta │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/home/kmaschta/.pm2/pm2.log last 20 lines:
PM2        | 2018-02-22 10:26:28: Time                 : Thu Feb 22 2018 10:26:28 GMT+0100 (CET)
PM2        | 2018-02-22 10:26:28: PM2 version          : 2.9.3
PM2        | 2018-02-22 10:26:28: Node.js version      : 8.9.4
PM2        | 2018-02-22 10:26:28: Current arch         : x64
PM2        | 2018-02-22 10:26:28: PM2 home             : /home/kmaschta/.pm2
PM2        | 2018-02-22 10:26:28: PM2 PID file         : /home/kmaschta/.pm2/pm2.pid
PM2        | 2018-02-22 10:26:28: RPC socket file      : /home/kmaschta/.pm2/rpc.sock
PM2        | 2018-02-22 10:26:28: BUS socket file      : /home/kmaschta/.pm2/pub.sock
PM2        | 2018-02-22 10:26:28: Application log path : /home/kmaschta/.pm2/logs
PM2        | 2018-02-22 10:26:28: Process dump file    : /home/kmaschta/.pm2/dump.pm2
PM2        | 2018-02-22 10:26:28: Concurrent actions   : 2
PM2        | 2018-02-22 10:26:28: SIGTERM timeout      : 1600
PM2        | 2018-02-22 10:26:28: ===============================================================================
PM2        | 2018-02-22 10:26:28: Starting execution sequence in -fork mode- for app name:test id:0
PM2        | 2018-02-22 10:26:28: App name:test id:0 online
PM2        | 2018-02-22 10:28:08: Stopping app:test id:0
PM2        | 2018-02-22 10:28:08: App [test] with id [0] and pid [17279], exited with code [0] via signal [SIGINT]
PM2        | 2018-02-22 10:28:08: pid=17279 msg=process killed
PM2        | 2018-02-22 10:28:08: Starting execution sequence in -fork mode- for app name:test id:0
PM2        | 2018-02-22 10:28:08: App name:test id:0 online

Kmaschta avatar Feb 22 '18 09:02 Kmaschta

Same here. Dirty workaround: delete and start again.

# Does not work
pm2 reload ecosystem.config.js --only MyApp --update-env

# Do work
pm2 delete MyApp
pm2 start ecosystem.config.js --only MyApp

webdif avatar Mar 01 '18 15:03 webdif

Confirmed,

Same here. Have to delete and start again.

zenz avatar Mar 03 '18 10:03 zenz

Same here. Any plans to change this behavior of update-env ?

joaonice avatar Oct 18 '18 09:10 joaonice

Just ran into this problem as well, it cascaded into some pretty serious issues for us. At the very least the feature should document what will happen, when it says "will update environment variables" I expect it to update fully, not just add new ones.

GriffinSauce avatar Nov 05 '18 09:11 GriffinSauce

Same here I had to delete and start the app 👎

mdwekat avatar Mar 25 '19 13:03 mdwekat

pretty difficult thing to remove already present variable in environment. Run pm2 delete app and then start the app again in this case. Sorry, I always try to make things easy but sometime that really not easy to implement. If anyone feel adventurous to make on PR on that, that is welcome.

Unitech avatar Apr 05 '19 14:04 Unitech

i actually think this is a good 'feature' that PM2 keeps environment variables across restarts. However I agree it should be documented - and in future - if this changes - please keep this 'feature'

here's my use case which works pretty awesome with this 'feature'.

We use Ansible to deploy our application - and when application is deployed (Ansible starts PM2) - passing in an environment variable call it FOO_BAR=hello ... application will start and query env FOO_BAR - and sees that it is set to hello (by ansible)

now if PM2 restarts (because application crashes or whatever) - env FOO_BAR is still set to hello ... pretty awesome ...

as above - please keep this 'feature' by document as appropriate. thx.

kerriganb avatar Aug 12 '19 22:08 kerriganb

I ran pm2 delete app and put the environment variable into the ecosystem.config.js which fixed it. But, then I tried passing it in from the command line and removing the setting from ecosystem.config.js. Back to same problem!

module.exports = {
  apps: [
    {
      name: "app",
      script: "./server.js",
      watch: "true",
      instances: 1,
      exec_mode: "cluster",
      autorestart: true,
      restart_delay: 10000,
      max_restarts: 6,
      env: {
        // not allowing to pass MONGO_PW from command line, keeps old PW if not placed here
        MONGO_PW: "password",
        NODE_ENV: "production"
      },
      max_memory_restart: "1G",
      node_args: ["--expose-gc", "--max-old-space-size=1024"],
      error_file: "app-error.log", // /home/dev/.pm2/logs/app-error-0.log
      out_file: "app-out.log",  // /home/dev/.pm2/logs/app-out-0.log
      log_file: "pm2.log" // /home/dev/.pm2/pm2.log
    }
]};

It is taking in SESS_SECRET from the command line but not MONGO_PW. Insists to keep an old PW. Checked it is nowhere used in source code.

robfr77 avatar Feb 06 '20 20:02 robfr77

Any updates? Facing the same issue. Can't remove env variable.

newmizanur avatar Aug 13 '21 09:08 newmizanur

bitten by this too...

dncpax avatar Apr 13 '22 14:04 dncpax

another victim of this issue

udisun avatar Apr 24 '22 10:04 udisun

facing the same issue, dont want down time, is there any way to refresh env without deleting my node app?

vdhruv avatar Oct 09 '23 15:10 vdhruv