How to get environment variables in live Heroku dyno

heroku run bash does the similar to heroku ps:exec but has the config vars available.


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.


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)."

Tags:

Bash

Heroku