5

I'd like to set a couple of environment variables on an Ubuntu machine (10.04), but I want to create their value via a script, much like:

export THE_ENV_VAR=$(script_to_execute_and_use_stdout_from)

I've tried setting in /etc/environment, but that only copies rhs verbatim

I've tried executing a script in /etc/init.d/ at startup, but that does not seem to work.

Ideas?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Robert
  • 2,330
  • 29
  • 47
  • Have you considered [Ask Ubuntu](http://askubuntu.com/) for this question? – Nano Taboada Oct 12 '11 at 20:18
  • @Nano: Nope, thanks for the hint. – Robert Oct 12 '11 at 20:43
  • Either way, why not trying to set it at `.bashrc`? I'd do something like `export THE_ENV_VAR='script_to_execute_and_use_stdout_from_within_backticks'` – Nano Taboada Oct 12 '11 at 23:09
  • @Nano: That will only set it for bash sessions and I need the env. variables to be set for all users (particularly www-data) – Robert Oct 13 '11 at 04:26
  • @Nano: Also, the script getting the values for the env. vars take some time to execute, that's why I want to do it at boot time, once only. – Robert Oct 13 '11 at 04:35
  • `/etc/bash.bashrc` applies to all users. More info [here](http://superuser.com/questions/49562/whats-the-difference-between-etc-bash-bashrc-and-bashrc-which-one-should-i) – Nano Taboada Oct 13 '11 at 13:26
  • @Robert - did you ever figure this out? I'm running into the same issue, and apparently not many people think that having a system-wide var is useful :/ which seems, to me, pretty much the opposite of true. – orokusaki Dec 20 '12 at 21:48
  • @orokusaki: Sort of, if you read the bottom answer (by m0ntassar), that is the way I do it now and it solves the problem. – Robert Dec 21 '12 at 11:57

1 Answers1

4

You need to write your export statement into /etc/bash.bashrc file, which is a system wide .bashrc file that will set environments for all system users :)

Edit: One way to do this is to populate a cache file during boot, and let the user scripts read from that cached file.

ambes
  • 5,272
  • 4
  • 26
  • 36
m0ntassar
  • 310
  • 1
  • 11
  • 1
    Yes, but is this run at boot time, or when a user connects via ssh ? The setting of the variables involves commands that take considerable time to execute, which is why I'd like them to be set at boot time. – Robert Oct 15 '11 at 04:54
  • 1
    This runs when a user connects via ssh (bash sessions of corse) – m0ntassar Oct 29 '11 at 11:32
  • Thanks, but that's not what I need. Interesting that there does not seem to be a way to accomplish this. – Robert Oct 29 '11 at 14:25
  • Here is a dirty way to do it : Add you script "script_to_execute_and_use_stdout_from" to boot and make it output results to a file Then, when a user connect, it reads the result from that file instead of executing the script, this should be much faster. – m0ntassar Oct 31 '11 at 10:06
  • Thanks m0ntassar, that is pretty much how I do it now, I have a kind of cache of the result so that the first user that connects populates the cache. At subsequent calls, it goes fast. Didn't think of running that at boot time, but you're right, it should work fine :) – Robert Oct 31 '11 at 20:30
  • Does not work for init scripts that try to read global variables. – Bevor Jan 16 '19 at 18:49