Forum: Ruby exec with environment?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5906a725dcd299ca2fcebd6deb06a2ef?d=identicon&s=25 Ian Peters-campbell (mahatmamanic)
on 2008-04-12 06:57
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?
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2008-04-12 07:15
(Received via mailing list)
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/
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-04-12 07:28
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-04-12 10:15
(Received via mailing list)
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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2008-04-12 16:23
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.
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2008-04-12 17:16
(Received via mailing list)
On Apr 12, 2008, at 8:23 AM, Marc Heiler 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/
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-04-12 17:45
(Received via mailing list)
On 12.04.2008 16:23, Marc Heiler 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
5906a725dcd299ca2fcebd6deb06a2ef?d=identicon&s=25 Ian Peters-campbell (mahatmamanic)
on 2008-04-15 23:24
Robert Klemme wrote:
> On 12.04.2008 16:23, Marc Heiler 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 :)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-04-15 23:42
(Received via mailing list)
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?

robert@fussel ~
$ echo $FOO


robert@fussel ~
$ ruby -e 'ENV["FOO"]="bar";exec("bash", "-c", "echo \$FOO")'
bar

robert@fussel ~
$ 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 :)

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

Kind regards

  robert
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2008-04-16 00:36
(Received via mailing list)
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 :)

If you are "permanently" changing the environment have you tried using
export to change the setting in the main program?
This topic is locked and can not be replied to.