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.
To display your current environment variables, use the env command. An environment variable that is accessible to all your processes is called a global variable.
As such, creating an . env file on Heroku isn't a good approach. Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable.
Heroku's environment variables are called config vars. To set a config var, navigate to the Settings tab of your app's dashboard, and click Reveal Config Vars.
To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.
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
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.
heroku run bash
does the similar to heroku ps:exec
but has the config vars available.
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 (i.e., env in a session will not list config vars set by heroku config:set
)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With