exec with environment?

  • Thread starter Ian Peters-campbell
  • Start date
I

Ian Peters-campbell

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?
 
A

ara.t.howard

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?

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/
 
J

Joel VanderWerf

Ian said:
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
 
R

Robert Klemme

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
 
M

Marc Heiler

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.
 
A

ara.t.howard

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/
 
R

Robert Klemme

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
 
I

Ian Peters-campbell

Robert said:
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 :)
 
R

Robert Klemme

Robert said:
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().

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
 
M

Michael W. Ryder

Ian said:
Robert said:
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 :)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top