Exec with environment?

I am running a Ruby script which needs to set some environment variables
and then execute non-Ruby programs through Linux and pass the modified
environment to the new processes. It doesn’t look as though I can do
this with ‘exec’, is there a way to do this with existing Ruby
functionality?

Ian Peters-campbell wrote:

I am running a Ruby script which needs to set some environment variables
and then execute non-Ruby programs through Linux and pass the modified
environment to the new processes. It doesn’t look as though I can do
this with ‘exec’, is there a way to do this with existing Ruby
functionality?

This doesn’t do what you want?

ENV[‘foo’] = ‘bar’
exec “echo $foo” # ==> bar

On 12.04.2008 06:57, Ian Peters-campbell wrote:

I am running a Ruby script which needs to set some environment variables
and then execute non-Ruby programs through Linux and pass the modified
environment to the new processes. It doesn’t look as though I can do
this with ‘exec’, is there a way to do this with existing Ruby
functionality?

Homegrown solution would be

new_env = {}

unless fork
ENV.clear # if needed
ENV.update new_env
exec…
end

Kind regards

robert

On Apr 12, 2008, at 8:23 AM, Marc H. wrote:

I believe something like
ENV[‘foo’] = ‘bar’
is enough.

You can try it, compile a small GNU configure program via ruby
ENV[‘CFLAGS’] = ‘O2’
should work, I am quite confident that it will work.

only if you are into clobbering the environment of the parent -
otherwise you need to push and pop the env or fork to start with a
fresh copy

just a note for posterity

a @ http://codeforpeople.com/

On Apr 11, 2008, at 10:57 PM, Ian Peters-campbell wrote:

I am running a Ruby script which needs to set some environment
variables
and then execute non-Ruby programs through Linux and pass the modified
environment to the new processes. It doesn’t look as though I can do
this with ‘exec’, is there a way to do this with existing Ruby
functionality?

Posted via http://www.ruby-forum.com/.

require ‘systemu’

systemu ‘foobar’, :env => hash

the session and open4 gems also both have this functionality. systemu
is cross platform, however.

cheers.

a @ http://codeforpeople.com/

On 12.04.2008 16:23, Marc H. wrote:

I believe something like
ENV[‘foo’] = ‘bar’
is enough.

This is not what the OP asked for IIRC. He wanted to modify the
environment of a process started from his Ruby program not the program’s
own environment. This question was likely inspired by the POSIX C
function /execle/.

You can try it, compile a small GNU configure program via ruby
ENV[‘CFLAGS’] = ‘O2’
should work, I am quite confident that it will work.

It will likely work but also affect the program’s own environment as
well as that of all subsequent started sub programs.

Kind regards

robert

Robert K. wrote:

On 12.04.2008 16:23, Marc H. wrote:

I believe something like
ENV[‘foo’] = ‘bar’
is enough.

This is not what the OP asked for IIRC. He wanted to modify the
environment of a process started from his Ruby program not the program’s
own environment. This question was likely inspired by the POSIX C
function /execle/.

You can try it, compile a small GNU configure program via ruby
ENV[‘CFLAGS’] = ‘O2’
should work, I am quite confident that it will work.

It will likely work but also affect the program’s own environment as
well as that of all subsequent started sub programs.

Kind regards

robert

Yes, apologies if my original question was not clear. I have a Ruby
script which starts a new session D-Bus, and then starts two other
applications which need to be connected to this new D-Bus. I have
control over the source code of one of these applications, and so can
order it to connect to a bus whose address I pass to it at startup. The
second application is effectively a black box though…I can only
control whether it connects to what it believes to be the system or
session bus.

Thus, I need a way to clobber its DBUS_SESSION_BUS_ADDRESS at startup.
I am already setting the ENV value on the parent process, but it is not
passing the modified environment to the process launched via exec().
So…I am looking for a way to push an environment to the “black box”
process while still managing to retain the PID in the parent (as I still
need to be able to signal the “black box.”

I hope that is more clear, and any help that people can provide would be
appreciated :slight_smile:

I believe something like
ENV[‘foo’] = ‘bar’
is enough.

You can try it, compile a small GNU configure program via ruby
ENV[‘CFLAGS’] = ‘O2’
should work, I am quite confident that it will work.

On 15.04.2008 23:24, Ian Peters-campbell wrote:

You can try it, compile a small GNU configure program via ruby
script which starts a new session D-Bus, and then starts two other
applications which need to be connected to this new D-Bus. I have
control over the source code of one of these applications, and so can
order it to connect to a bus whose address I pass to it at startup. The
second application is effectively a black box though…I can only
control whether it connects to what it believes to be the system or
session bus.

Thus, I need a way to clobber its DBUS_SESSION_BUS_ADDRESS at startup.
I am already setting the ENV value on the parent process, but it is not
passing the modified environment to the process launched via exec().

Are you sure?

[email protected] ~
$ echo $FOO

[email protected] ~
$ ruby -e ‘ENV[“FOO”]=“bar”;exec(“bash”, “-c”, “echo $FOO”)’
bar

[email protected] ~
$ echo $FOO

So…I am looking for a way to push an environment to the “black box”
process while still managing to retain the PID in the parent (as I still
need to be able to signal the “black box.”

I hope that is more clear, and any help that people can provide would be
appreciated :slight_smile:

It was pretty clear to me and I believe I provided a solution already
(see my earlier posting).

Kind regards

robert

Ian Peters-campbell wrote:

You can try it, compile a small GNU configure program via ruby
script which starts a new session D-Bus, and then starts two other
So…I am looking for a way to push an environment to the “black box”
process while still managing to retain the PID in the parent (as I still
need to be able to signal the “black box.”

I hope that is more clear, and any help that people can provide would be
appreciated :slight_smile:

If you are “permanently” changing the environment have you tried using
export to change the setting in the main program?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs