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?

8
  • Have you considered Ask Ubuntu for this question? Commented Oct 12, 2011 at 20:18
  • @Nano: Nope, thanks for the hint. Commented Oct 12, 2011 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' Commented Oct 12, 2011 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) Commented Oct 13, 2011 at 4: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. Commented Oct 13, 2011 at 4:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
This runs when a user connects via ssh (bash sessions of corse)
Thanks, but that's not what I need. Interesting that there does not seem to be a way to accomplish this.
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.
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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.