# Getstatusoutput

**URL:** <https://www.ruby-forum.com/t/getstatusoutput/71261>\
**Category:** Ruby\
**Created:** [October 6, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/getstatusoutput/71261 "2006-10-06T15:07:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 6, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/getstatusoutput/71261/1 "2006-10-06T15:07:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 6, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/getstatusoutput/71261/2 "2006-10-06T15:41:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 6, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/getstatusoutput/71261/3 "2006-10-06T18:20:37Z")

</div>

removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 6, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/getstatusoutput/71261/4 "2006-10-06T18:40:39Z")

</div>

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:in`all\_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:in`spawn’  
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:in`spawn’  
from /home/ahoward//lib/ruby/1.8/timeout.rb:45:in `timeout' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:272:in`spawn’  
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! 😉

-a
