简体   繁体   English

如何在实时 Heroku dyno 中获取环境变量

[英]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.有一个heroku config命令,但这显然只是告诉我当前的设置是什么。 I want to confirm in a dyno what my application is actually seeing in the running environment.我想在 dyno 中确认我的应用程序在运行环境中实际看到的内容。

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).我尝试了heroku ps:exec -a <app> -d <dyno_instance> --ssh env并且它有一些通用输出(如SHELLPATH等)输出,但它没有显示我配置的任何环境变量(例如我的数据库字符串)。 I've also tried directly logging in (using bash instead of the env command) and poked around but couldn't find anything.我也试过直接登录(使用bash而不是env命令)并四处寻找但找不到任何东西。

Try heroku run env instead.尝试使用heroku run env代替。

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 Exec 创建的 SSH 会话不会将配置变量设置为环境变量(即,会话中的 env 不会列出由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).但是, heroku run将启动一个新的dyno,因此您只需要检查正在运行的dyno 的实际环境是不够的(假设,假设Heroku 出现故障并且无法启动新的dyno)。

Here's one way to check the environment of a running dyno:这是检查正在运行的 dyno 环境的一种方法:

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

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

  • Get the pid of your server process (check your Procfile if you don't know).获取您的服务器进程的 pid(如果您不知道,请检查您的 Procfile)。 Let's say you're using puma : ps aux | grep puma假设您正在使用pumaps 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)选择第一个(4,第二列中的第一个数字,在本例中)

  • Now, you can get the environment of that process.现在,您可以获得该过程的环境。 Replace <PID> by the process id you just got, for example 4 :<PID>替换为您刚刚获得的进程 ID,例如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. tr是为了更容易阅读,因为/proc/<pid>/environ是零分隔的。

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.如果您的 Heroku 堆栈支持 Node.js,那么您可以在 Heroku 应用程序上运行 Node.js 进程并打印所有(而不仅仅是您配置的)环境变量。

Commands:命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行子进程后如何获取环境变量 - How to get environment variables after running a subprocess 如何将环境变量放入 bash 数组? - How to get environment variables into a bash array? 导出本地开发和heroku部署的环境变量 - Exporting environment variables for local development and heroku deployment 在 Heroku 上为 Sinatra 应用设置环境变量 - Setting environment variables for a Sinatra app on Heroku 如何从 Heroku 为单个 bash 命令设置环境变量,而不是为整个 shell Z21D9F4440CFB0E51508A 设置环境变量? - How can I set environment variables from Heroku for a single bash command, not for the whole shell session? 如何将环境变量放入cloud-init运行的Ansible脚本中? - How to get environment variables into Ansible script run by cloud-init? 如何获取C程序本身设置的所有环境变量? - How to get all environment variables set by a C program itself? 如何使postgresql在我的本地环境中工作? 我想构建并部署到heroku。 - How do I get postgresql to work in my local environment? I want to build and deploy to heroku. 如何在javascript中设置环境变量 - How to set environment variables in javascript 如何打印/回显环境变量? - How to print / echo environment variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM