2

Consider the following Ruby code

sleep 10
puts "Foo is #{ENV['foo']}"

Saving this file to envtest.rb

Running this from the shell:

export foo=bar
ruby envtest.rb &
export foo=baz
( ... 10 seconds later ... )
=> Foo is bar

It appears that the environment is evaluated when the ruby interpreter is launched. Is it possible to update environment variables during execution and have those changes reflected in running processes? If so, how?

tchrist
  • 78,834
  • 30
  • 123
  • 180
Niels B.
  • 5,912
  • 3
  • 24
  • 44
  • Related and probably duplicate of one or another of: http://stackoverflow.com/q/2967762 http://stackoverflow.com/q/205064 http://stackoverflow.com/q/9360679 http://stackoverflow.com/q/6094805 http://stackoverflow.com/q/263005 – tchrist Jul 23 '13 at 16:52

2 Answers2

5

You can change the value during runtime - from inside the ruby script - using:

ENV['VARIABLE_NAME'] = 'value'

There is no option to change environment values from outside the process after it has been started. That's by design, as the environment will be passed at process startup.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
2

No. This is not possible. One process can never directly manipulate the environment of a different already-running process. All you can ever do is set the environment on unborn children, then create them.

The only other approach is via active, negotiated communication back to the parent. That’s why the output from tset(1) (that is, of tset -s) is always evaluated by the parent.

tchrist
  • 78,834
  • 30
  • 123
  • 180