Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get environment variables in live Heroku dyno

Tags:

bash

heroku

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 envand 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.

like image 805
s g Avatar asked Jan 05 '18 18:01

s g


People also ask

How can I see my current environment variables?

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.

Does .env work on Heroku?

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.

How do I access Heroku config vars?

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.

How do I get a list of environment variables?

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.


3 Answers

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.

like image 108
André Laszlo Avatar answered Sep 27 '22 16:09

André Laszlo


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

like image 26
Merlin Ran Avatar answered Sep 27 '22 18:09

Merlin Ran


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

like image 23
David Avatar answered Sep 27 '22 16:09

David