# Executing program from ruby

**URL:** <https://www.ruby-forum.com/t/executing-program-from-ruby/92443>\
**Category:** Ruby\
**Created:** [April 19, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443 "2007-04-19T15:22:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 19, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/1 "2007-04-19T15:22:48Z")

</div>

Hello i know there are several ways to execute from within ruby  
but each one have their own problems:

`` doesn’t check if command /programm runs at all!, also it blocks  
untill  
command returns, no bi-directional comunication  
popen2,3,4 doesn’t check if command exists at all … it’s not rising  
any  
errors … and happilly continues to run untill you try to write  
something to  
pipe that leads to program that never even started

only system() checks if command runs but it also returns false when  
programs  
starts but returns anything other then 0

does anyone know good way to check if command exists then run it, return  
pid  
for monitoring purposes and allow bidirectional comunication, then check  
exit  
status ?

open4 comes close to fulfilling all that but it doesn’t rise (sample  
program  
that tells you it’ll doesn’t eaither), not to mention compleate lack of  
documentation.

any expirience with open4? help? pointers?  
mayby i’m simply missing some obvious way?

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 19, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/2 "2007-04-19T15:30:51Z")

</div>

On 19.04.2007 15:21, Marcin R. wrote:

> starts but returns anything other then 0  
> mayby i’m simply missing some obvious way?  
> popen and its derivatives?

robert

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 19, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/3 "2007-04-19T15:35:52Z")

</div>

On Apr 19, 2007, at 8:21 AM, Marcin R. wrote:

> any expirience with open4? help? pointers?

TextMate uses a hand-rolled popen3(), which is really just an open4()  
clone. Allan Odgaard, the creator of TextMate, wrote the code, but  
it’s straightforward enough that I thought it might help you to see it:

def my\_popen3(\*cmd) # returns [stdin, stdout, strerr, pid]  
pw = IO::pipe # pipe[0] for read, pipe[1] for write  
pr = IO::pipe  
pe = IO::pipe

pid = fork{  
pw[1].close  
STDIN.reopen(pw[0])  
pw[0].close

```
 pr[0].close
 STDOUT.reopen(pr[1])
 pr[1].close

 pe[0].close
 STDERR.reopen(pe[1])
 pe[1].close

 exec(*cmd)

```

}

pw[0].close  
pr[1].close  
pe[1].close

pw[1].sync = true

[pw[1], pr[0], pe[0], pid]  
end

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 19, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/4 "2007-04-19T15:50:56Z")

</div>

On Thursday 19 April 2007 13:34, James Edward G. II wrote:

> pe = IO::pipe  
> pe[0].close  
> pw[1].sync = true
> 
> [pw[1], pr[0], pe[0], pid]  
> end
> 
> Hope that helps.
> 
> James Edward G. II

code is really simmilar to opn4 i understand how it works (i follow  
"don’t use  
wizard/library you don’t understand " rule) … and to be honest i can  
write  
it myself, but i thought there’s solution for problem already :]

well popen 3/4 is almost good enought … but if you use it with block it  
doesn’t check if command was executed at all!!!

i need to do something like this:

check if command exists…  
try to run it…  
if there’s problem (no privilages etc.) do some work about that , notify  
user  
etc.  
if program started write to it’s stdin and get answer.

popen4 should do exactly that … but sample that should rise error  
doesn’t do  
that, not to mention there’s no documentation except changelog and few  
samples.

thnx for reply tho

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 20, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/5 "2007-04-20T15:47:14Z")

</div>

On Thursday 19 April 2007 15:04, removed\_email\_address@domain.invalid wrote:

> > only system() checks if command runs but it also returns false when
> 
> file or directory - no-exist (Errno::ENOENT) from  
> /home/ahoward/eg/ruby/open4/open4-0.9.3  
> /home/ahoward/nfs//lib/ruby/site\_ruby/1.8/open4.rb:37:in `popen4’ from  
> ??
> 
> > mayby i’m simply missing some obvious way?
> 
> i wrote open4 - so i’m expirienced with it 😉 can you give and example of  
> exactly what you’re trying to do?
> 
> regards.
> 
> -a

i sent you example on e-mail, but it was my fault… in you examples  
there’s  
no require ‘rubygems’, and it’s required to work, if not it loads  
standard  
open4 library 😕 took me 2h to figure that out.

preaty good library tho (at least when i got it to work)

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [April 19, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/executing-program-from-ruby/92443/6 "2007-04-19T17:05:12Z")

</div>

On Thu, 19 Apr 2007, Marcin R. wrote:

> starts but returns anything other then 0
> 
> does anyone know good way to check if command exists then run it, return pid  
> for monitoring purposes and allow bidirectional comunication, then check exit  
> status ?
> 
> open4 comes close to fulfilling all that but it doesn’t rise (sample program  
> that tells you it’ll doesn’t eaither),

??

harp:~ \> ruby -r open4 -e’ Open4.popen4 “no-exist” ’  
/home/ahoward//lib/ruby/site\_ruby/1.8/open4.rb:37:in `exec': No such file or directory - no-exist (Errno::ENOENT) from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:37:in `popen4’  
from /home/ahoward//lib/ruby/site\_ruby/1.8/open4.rb:21:in  
`popen4’  
from -e:1

harp:~ \> ruby -r open4 -e’ Open4.spawn “no-exist” ’  
/home/ahoward//lib/ruby/site\_ruby/1.8/open4.rb:37:in `exec': No such file or directory - no-exist (Errno::ENOENT) from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:37:in `popen4’  
from /home/ahoward//lib/ruby/site\_ruby/1.8/open4.rb:21: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:38:in `timeout' from /home/ahoward//lib/ruby/site_ruby/1.8/open4.rb:272:in `spawn’  
from -e:1

from the dist

harp:open4-0.9.3 \> pwd  
/home/ahoward/eg/ruby/open4/open4-0.9.3

harp:open4-0.9.3 \> cat sample/exception.rb  
require “open4”  
Open4::popen4 “noexist”

harp:open4-0.9.3 \> ruby sample/exception.rb  
/home/ahoward/nfs//lib/ruby/site\_ruby/1.8/open4.rb:37:in `exec': No such file or directory - noexist (Errno::ENOENT) from /home/ahoward/nfs//lib/ruby/site_ruby/1.8/open4.rb:37:in `popen4’  
from /home/ahoward/nfs//lib/ruby/site\_ruby/1.8/open4.rb:21:in  
`fork' from /home/ahoward/nfs//lib/ruby/site_ruby/1.8/open4.rb:21:in `popen4’  
from sample/exception.rb:3

can you show an exampe which does not work?

> not to mention compleate lack of \> documentation.

??

[http://codeforpeople.com/lib/ruby/open4/open4-0.9.3/README](http://codeforpeople.com/lib/ruby/open4/open4-0.9.3/README)

this covers nearly every part of open4. if you need more, there is

[http://codeforpeople.com/lib/ruby/open4/open4-0.9.3/lib/open4.rb](http://codeforpeople.com/lib/ruby/open4/open4-0.9.3/lib/open4.rb)

it is an extremely small library

> any expirience with open4? help? pointers?  
> mayby i’m simply missing some obvious way?

i wrote open4 - so i’m expirienced with it 😉 can you give and example  
of  
exactly what you’re trying to do?

regards.

-a
