PM2 environment variables caching
Asked Answered
A

1

8

I am running PM2 on Ubuntu 16.04 and it seems that environment variables are being cached somehow. Is there an way of seeing which environment variables PM2 are using. The environment variables it can somehow see are not available in my terminal session anymore echo $VAR_NAME.

I created the environment variables like this:

export VAR_NAME=value

Removing the environment variable using:

unset VAR_NAME

doesn't work PM2 is stubbornly holding on to the environment variable - even after various restart & ssh sessions. Render me confused :-/

Is there a way of flushing the environment variables PM2 is using? Or at least seeing which environment variables it knows about?

Abominate answered 29/9, 2016 at 20:31 Comment(4)
Did you try pm2 kill and then pm2 start app.js? This way pm2 would starting in a new state.Freund
No that is one thing I didn't try. It seems a bit like a sledge hammer approach, but it is working. It's removing all the environment variables. It would be great if there was a way to see which variables PM2 is caching.Erebus
I added an answer below including an issue from PM2's repo, it seems this is the only way. Once PM2 is started, their is no way to change the variables' values or unset them.Freund
You might want to check out the update in my answer below, it was quite useful for me to find out about it, and I thought I would share it here.Freund
F
18

Update to the original answer:

If the environment variables' values are preset, as in the case of having different env variables for development, staging, and production, there is an option using a process.json file.

The below is a sample for a node.js app:

{
  "apps" : [{
    "env": { 
     // in this section you would list variables that you 
     // want available in all cases
      "NODE_PATH": "..."
    },
    "env_development": {
      "CONFIG": "dev.conf.json",
      "NODE_ENV": "development"
    },
    "env_production" : {
       "CONFIG": "conf.json",
       "NODE_ENV": "production"
    },
    "exec_mode": "fork", // or cluster if that's what you want
    "name"        : "my_app",
    "script"      : "/var/my_app/app.js", //srcipt's path
    "watch"       : false // don't restart on file changes
  }]
}

Having this file defined, with the possible values for env, you can switch environment by restarting the app as follows:

  1. Start the app normally: pm2 start process.json --env development

  2. When you want to switch to a different env: pm2 restart process.json --env production

For more about process.json and the possible options: PM2 - Process File


Original answer:

You have to kill pm2 first.

pm2 kill

pm2 start app.js

PM2 preserves the environment variables it read upon starting, it does not reread their values every time.

I searched for it quickly, and found this issue on github: https://github.com/Unitech/pm2/issues/83, and Unitech's answers confirm this.

In this particular comment: https://github.com/Unitech/pm2/issues/83#issuecomment-29837221

Unitech says:

Yep this is normal in "cluster_mode". As pm2 wrap your code to his own context (and own variables) you get what was already there when launching pm2.

Freund answered 29/9, 2016 at 20:41 Comment(1)
pm2 kill what I needed. pm2 was using old paths and would error on start, that I saw in ~/.pm2/logs/logs-error.log which led to this thread!!Clayborne

© 2022 - 2024 — McMap. All rights reserved.