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?

1

2 Answers 2

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.

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.