简体   繁体   中英

How to get environment variables in live Heroku dyno

There is a heroku config command but that apparently just shows me what the current setting is. I want to confirm in a dyno what my application is actually seeing in the running environment.

I tried heroku ps:exec -a <app> -d <dyno_instance> --ssh env and this has some generic output (like SHELL , PATH , etc.) output but it doesn't show any env vars that I've configured (like my db strings, for example). I've also tried directly logging in (using bash instead of the env command) and poked around but couldn't find anything.

Try heroku run env instead.

According to the documentation : "The SSH session created by Heroku Exec will not have the config vars set as environment variables (ie, env in a session will not list config vars set by heroku config:set )."

heroku run bashheroku ps:exec类似,但有可用的配置变量。

The accepted answer is okay in most cases. heroku run will start a new dyno however, so it won't be enough you need to check the actual environment of an running dyno (let's say, purely hypothetically, that Heroku has an outage and can't start new dynos).

Here's one way to check the environment of a running dyno:

  • Connect to the dyno: heroku ps:exec --dyno <dyno name> --app <app name>

    For example: heroku ps:exec --dyno web.1 --app my-app

  • Get the pid of your server process (check your Procfile if you don't know). Let's say you're using puma : ps aux | grep puma ps aux | grep puma

    The output might look something like this:

     u35949 4 2.9 0.3 673980 225384 ? Sl 18:20 0:24 puma 3.12.6 (tcp://0.0.0.0:29326) [app] u35949 31 0.0 0.0 21476 2280 ? S 18:20 0:00 bash --login -c bundle exec puma -C config/puma.rb u35949 126 0.1 0.3 1628536 229908 ? Sl 18:23 0:00 puma: cluster worker 0: 4 [app] u35949 131 0.3 0.3 1628536 244664 ? Sl 18:23 0:02 puma: cluster worker 1: 4 [app] u35949 196 0.0 0.0 14432 1044 pts/0 S+ 18:34 0:00 grep puma

    Pick the first one (4, the first number in the second column, in this example)

  • Now, you can get the environment of that process. Replace <PID> by the process id you just got, for example 4 :

     cat /proc/<PID>/environ | tr '\\0' '\\n' HEROKU_APP_NAME=my-app DYNO=web.1 PWD=/app RACK_ENV=production DATABASE_URL=postgres://... ...

    The tr is there to make it easier to read, since the contents of /proc/<pid>/environ is zero-delimited.

If your Heroku stack supports Node.js, then you can run a Node.js process on your Heroku app and print all (and not just the ones, that you configured) environment variables.

Commands:

  1. heroku run node --app your-heroku-app-name
  2. console.log(process.env)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM