Getstatusoutput

Dear Ruby fellows,

I am desperately trying to write a quite simple function (something
similar to what I was acumstomed to with Python)

status, stdout, stderr = getstatusoutput(somecmdstring)

status is an integer, stdout and stderr are strings

Now in principle I know how to write such a function, but I don’t know
what the best interface to spawn the process given in somecmdstring is.
Here is my analysis of the situation:

IO::popen — mixes stdout and stderr
Open3.popen3 — no possibility to wait for the process to finish
exec — I would have to write “everything” by myself, double fork,
etc.
…popen4 — not a standard function

Is my analysis right, or is there something I am not understanding
about open3?

thanks for any help,
Oliver

On Fri, 6 Oct 2006, OliverMarchand wrote:

what the best interface to spawn the process given in somecmdstring is.
Here is my analysis of the situation:

IO::popen — mixes stdout and stderr
Open3.popen3 — no possibility to wait for the process to finish
exec — I would have to write “everything” by myself, double fork,
etc.
…popen4 — not a standard function

Is my analysis right, or is there something I am not understanding
about open3?

your analysis is correct, it’s why i wrote open4. however, you cannot
wait on
the program with ‘getstatusoutput’ either - at least i don’t see the
child pid
in there?

do you mean you just need the exit_status? i assume so given the
interface
above.

the thing with using open4 is that you can also do things like

status = spawn ‘echo’, :stdin => ‘foobar’

you’re free to steal the code too - the liscense is ruby’s. otherwise

gem install open4

regards.

-a

[email protected] schrieb:

your analysis is correct, it’s why i wrote open4. however, you cannot wait on
the program with ‘getstatusoutput’ either - at least i don’t see the child pid
in there?

do you mean you just need the exit_status? i assume so given the interface
above.

Thanks for your quick answer.

I was assuming that my getstatusoutput returns after the process has
finished. I was wanting to add a timeout parameter and an exception for
overly long executions, but left that discussion out of the original
post to make it as simple as possible.

Thanks for hinting open4, the only thing I am wondering is the best
method to wait for pid to finish!? Now I can write some “while pid
exists do nothing”, but doen’t that consume unnecessary cyles?

Thanks, Oliver

On Sat, 7 Oct 2006, OliverMarchand wrote:

I was assuming that my getstatusoutput returns after the process has
finished. I was wanting to add a timeout parameter and an exception for
overly long executions, but left that discussion out of the original post to
make it as simple as possible.

Thanks for hinting open4, the only thing I am wondering is the best method
to wait for pid to finish!? Now I can write some “while pid exists do
nothing”, but doen’t that consume unnecessary cyles?

using open4? it’s already written for you:

 harp:~ > cat a.rb
 require 'open4'
 include Open4

 stdin, stdout, stderr = 'foobar', '', ''
 status = spawn 'cat', :stdin=>stdin, :stdout=>stdout, 

:stderr=>stderr, :timeout=>4
puts stdout

 stdin, stdout, stderr = 'foobar', '', ''
 status = spawn 'sleep 10 && cat', :stdin=>stdin, :stdout=>stdout, 

:stderr=>stderr, :timeout=>4
puts stdout

 harp:~ > ruby a.rb
 foobar
 /home/ahoward//lib/ruby/1.8/timeout.rb:43:in `pop': execution 

expired (Timeout::Error)
from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:151:in
all_done' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:151:inall_done’
from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:144:in
run' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:295:inspawn’
from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:65:in
popen4' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:273:inspawn’
from /home/ahoward//lib/ruby/1.8/timeout.rb:45:in timeout' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:272:inspawn’
from a.rb:8

if you really don’t want to install it at least steal the code - it’s
criminal
to re-write stuff like this! :wink:

-a