s g
s g

Reputation: 5667

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

Upvotes: 13

Views: 5766

Answers (4)

Benny Code
Benny Code

Reputation: 54975

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)

Upvotes: 0

Andr&#233; Laszlo
Andr&#233; Laszlo

Reputation: 15537

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.

Upvotes: 3

Merlin Ran
Merlin Ran

Reputation: 403

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

Upvotes: 9

David
David

Reputation: 4857

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

Upvotes: 11

Related Questions