Reloading with a new environment and --update-env does not update the environment
Edit: I didn't realize I had already posted this a few weeks ago https://github.com/Unitech/pm2/issues/3164. @vmarchaud you gave the same solution but the solution doesn't work. :(
What's going wrong?
I have my staging and production environments setup in my ecosystem.config.js. When I run the following, my environment is still stuck on staging instead of production.
pm2 start ecosystem.config.js --env staging
pm2 reload all --env production --update-env
How could we reproduce this issue?
Edit: Here is a cloneable repo to reproduce the issue
- Set app to print the
process.env.NODE_ENVvariable
server.js
console.log(process.env.NODE_ENV);
- Setup 2 environments in ecosystem.config.js
module.exports = {
apps: [
// First application
{
name: 'app',
script: 'server.js',
instances: 'max',
exec_mode: 'cluster',
error_file: 'err.log',
out_file: 'app.log',
merge_logs: false,
env: {
NODE_ENV: 'development',
},
env_staging: {
NODE_ENV: 'staging',
},
env_production: {
NODE_ENV: 'production',
},
},
],
};
- Start app in environment 1
./node_modules/.bin/pm2 start ecosystem.config.js --env staging
- Reload app in environment 2
./node_modules/.bin/pm2 reload all --env production --update-env
- Cat one of the app files
$ cat app-1.log
staging
staging
The second line should say production because the environment was updated.
Workaround
Once the process is already started on env X, to get it to start on env Y
-
Kill pm2 instances
./node_modules/.bin/pm2 stop all -
Kill pm2 god daemon
pkill -9 PM2 -
Start pm2 in the correct environment
./node_modules/.bin/pm2 reload all --env production --update-env
Supporting information
===============================================================================
--- PM2 REPORT (Wed Oct 04 2017 12:42:48 GMT-0400 (EDT)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 2.7.0
node version : 8.4.0
node path : /Users/user/projects/app/tmp/../node_modules/.bin/pm2
argv : /Users/user/.nvm/versions/node/v8.4.0/bin/node,/Users/user/projects/app/node_modules/pm2/lib/Daemon.js
argv0 : node
user : user
uid : 692055800
gid : 1739585065
uptime : 1min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 2.7.0
node version : 8.4.0
node path : /Users/user/.nvm/versions/node/v8.4.0/bin/pm2
argv : /Users/user/.nvm/versions/node/v8.4.0/bin/node,/Users/user/.nvm/versions/node/v8.4.0/bin/pm2,report
argv0 : node
user : user
uid : 692055800
gid : 1739585065
===============================================================================
--- System info --------------------------------------------
arch : x64
platform : darwin
type : Darwin
cpus : Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
cpus nb : 8
freemem : 70680576
totalmem : 17179869184
home : /Users/user
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────┼──────────┤
│ app │ 0 │ cluster │ 68381 │ online │ 1 │ 32s │ 0% │ 32.7 MB │ user │ disabled │
│ app │ 1 │ cluster │ 68382 │ online │ 1 │ 32s │ 0% │ 32.8 MB │ user │ disabled │
│ app │ 2 │ cluster │ 68418 │ online │ 1 │ 29s │ 0% │ 33.1 MB │ user │ disabled │
│ app │ 3 │ cluster │ 68419 │ online │ 1 │ 29s │ 0% │ 33.4 MB │ user │ disabled │
│ app │ 4 │ cluster │ 68455 │ online │ 1 │ 26s │ 0% │ 34.1 MB │ user │ disabled │
│ app │ 5 │ cluster │ 68456 │ online │ 1 │ 26s │ 0% │ 33.5 MB │ user │ disabled │
│ app │ 6 │ cluster │ 68492 │ online │ 1 │ 22s │ 0% │ 33.0 MB │ user │ disabled │
│ app │ 7 │ cluster │ 68493 │ online │ 1 │ 22s │ 0% │ 33.8 MB │ user │ disabled │
└──────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/Users/user/.pm2/pm2.log last 20 lines:
PM2 | 2017-10-04 12:42:25: Stopping app:app id:_old_4
PM2 | 2017-10-04 12:42:25: Stopping app:app id:_old_5
PM2 | 2017-10-04 12:42:25: App name:app id:_old_4 disconnected
PM2 | 2017-10-04 12:42:25: App [app] with id [_old_4] and pid [68012], exited with code [0] via signal [SIGINT]
PM2 | 2017-10-04 12:42:25: App name:app id:_old_5 disconnected
PM2 | 2017-10-04 12:42:25: App [app] with id [_old_5] and pid [68030], exited with code [0] via signal [SIGINT]
PM2 | 2017-10-04 12:42:25: pid=68012 msg=process killed
PM2 | 2017-10-04 12:42:25: Starting execution sequence in -cluster mode- for app name:app id:6
PM2 | 2017-10-04 12:42:25: pid=68030 msg=process killed
PM2 | 2017-10-04 12:42:25: Starting execution sequence in -cluster mode- for app name:app id:7
PM2 | 2017-10-04 12:42:26: App name:app id:6 online
PM2 | 2017-10-04 12:42:26: App name:app id:7 online
PM2 | 2017-10-04 12:42:29: Stopping app:app id:_old_6
PM2 | 2017-10-04 12:42:29: Stopping app:app id:_old_7
PM2 | 2017-10-04 12:42:29: App name:app id:_old_6 disconnected
PM2 | 2017-10-04 12:42:29: App [app] with id [_old_6] and pid [68048], exited with code [0] via signal [SIGINT]
PM2 | 2017-10-04 12:42:29: App name:app id:_old_7 disconnected
PM2 | 2017-10-04 12:42:29: App [app] with id [_old_7] and pid [68066], exited with code [0] via signal [SIGINT]
PM2 | 2017-10-04 12:42:29: pid=68048 msg=process killed
PM2 | 2017-10-04 12:42:29: pid=68066 msg=process killed
Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues
@vmarchaud is this a bug?
I am seeing lots of environment issues as well with this version of PM2. It appears to be ignoring anything in the "env_" fields.
Please read the docs here, its explained :
You want to restart your process and pick-up changes in your ecosystem.json ? Use pm2 reload ecosystem.json --update-env
Thanks!, I found the docs shortly after writing this, but couldn't find my thread. I was not expecting this update it looks like.
-- [image: Giftnix]
Brandon Copley
Founder & CEO
t: 512.784.6060 <(512)%20784-6060>
On Tue, Oct 10, 2017 at 3:37 PM, vmarchaud [email protected] wrote:
Closed #3192 https://github.com/Unitech/pm2/issues/3192.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Unitech/pm2/issues/3192#event-1286895447, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJShhySWz2wqYpkDvPZJuEcQCFRNR9wks5sq9WBgaJpZM4Ptu2k .
@vmarchaud Im not sure if I understand. If I launch the app in env X and I want to reload the app in env Y, using --env Y --update-env, it does not seem to work. It will maintain env X unless I kill the PM2 god daemon and restart pm2 with the env Y.
Do my reproduction steps make sense?
@vmarchaud On step 4 in my original post, my command is equivalent to the one that you suggested from within the docs. Shouldn't this command reload pm2 in the correct environment with my above ecosystem config?
./node_modules/.bin/pm2 reload all --env production --update-env
The top comment on the PM2 - Process File page in Disqus has a thread of people with the same issue. They suggest this command instead.
./node_modules/.bin/pm2 startOrGracefulReload prod.json --env production --update-env
Edit: works fine now... how strange... Turns out that using reload all was the culprit. Using the below command worked fine.
./node_modules/.bin/pm2 reload ecosystem.config.js --env production --update-env
It seems pm2 startOrReload ecosystem.config.js --update-env not working.
But pm2 restart ... works.
@vmarchaud judging by comments and votes it seems to be an issue. Do you mind reopening this ticket?
Any updates on this issue? --update-env still doesn't work.
Please re-open the issue.
I will try to make some tests soon. If I understand option --update-env works with restart command but when we use ecosystem.config.js it doesn't update env. Am I right ?
Hi! Thanks for a quick reply!
Unfortunately nope, --update-env doesn't work neither with restart nor with reload, nor gracefull..., startOr... and so on...
We're trying to make deployment on our server, the directory structure is something like:
v1/
v2/
v3/
current -> ./v3 (symlink)
In the beginning I thought that the problem is in a symlink, but then I've changed the directory to v2/ directly then run pm2 start ecosystem.config.js and when I'm cding to v3/ and trying to restart pm2 ecosystem, it still starts files from the v2/ directory. Changing any file's contents, changing version in package.json doesn't help.
Yay! I found this article and make it work!
http://pm2.keymetrics.io/docs/tutorials/capistrano-like-deployments
The key difference is that I had to explicitly set cwd option in my ecosystem.config.js to /an/absolute/path/to/current (the symlink).
Thus,
pm2 startOrGracefulReload current/ecosystem.config.js --update-env
works gracefully as expected!
I'm sorry, it was my bad.
Thanks for your attention! You're doing a great job!
we have cwd , but startOrReload not work
Try so many but this works pm2 restart 1 --update-env
or Better still pm2 reload 1 --update-env
Both allow you to pick up the changes on your env ASAP
I can't get any combination of the above suggestions to make --update-env work, even with arbitrarily simple "hello world" examples.
The only way I can get pm2 to pick up on a config change is to delete a specific app process or kill the daemon entirely. I guess I'll just add that to my deploy scripts.
@delino12 the restart didn't work for me. I wrote up a quick proof of concept repo here using pm2 3.0.3.
@nitrocode you have to kill the server. addition to that, app will pickup the new changes when ever a server is restarted. Please make sure you restart or reload both the .env and the application files changes.
@delino12 by server, do you mean the pm2 daemon, or a managed node process?
I'm still trying to suss out whether this is a documentation bug, a widespread misunderstanding, or the --update-env flag just straight-up not working as advertised.
@delino12 I tried both of these commands and they did not start the app in the intended environment
pm2 reload all --env production --update-env
pm2 restart all --env production --update-env
I tried replacing all with 0 and that also did not work. Can you share the full command you used? Try the repo I posted above if you get a chance.
@nitrocode here is a quick step to handle env process I will recommend you stop all app process first, make sure you stop the process from running, most application process is still running on the old env variables because the process is still running and server has cache the old env variables which will make changes not effective.
pm2 restart all --update-env
pm2 restart [appname] --update-env
http://pm2.keymetrics.io/docs/usage/environment