# Invoking External Program

**URL:** <https://www.ruby-forum.com/t/invoking-external-program/67439>\
**Category:** Ruby\
**Created:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/invoking-external-program/67439 "2006-09-04T14:43:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/invoking-external-program/67439/1 "2006-09-04T14:43:53Z")

</div>

Hi

I’ve been able to launch an external program and read standard output  
using the following code.

IO.popen(“program.exe”, ‘r+’) do |io|  
io.puts “command line input”  
io.close\_write  
puts io.read  
end

1. How would I add a timeout to the command (e.g. 10 seconds)?
2. How would I check the error level (e.g. 0, 1, etc)?
3. How would I read standard error?

Thanks

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/invoking-external-program/67439/2 "2006-09-04T14:44:03Z")

</div>

On 9/3/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. How would I add a timeout to the command (e.g. 10 seconds)?
> 2. How would I check the error level (e.g. 0, 1, etc)?
> 3. How would I read standard error?

Just quick, uncomplete answers (from .exe I guess you are on windows):

1. try using Timeout.timeout [1] but I’m not sure how successful  
you’ll be - when the program blocks then you probably won’t be able to  
do anything. When the timeout elapses, you’d need to kill the process.

2. use $? [2]

3. look for popen3 [3] or redirect stderr to stdout or a file. I’m not  
sure whether popen3 works on windows.

[1] [http://ruby-doc.org/core/classes/Timeout.html#M001924](http://ruby-doc.org/core/classes/Timeout.html#M001924)  
[2] [Ruby | zenspider.com | by ryan davis](http://www.zenspider.com/Languages/Ruby/QuickRef.html#18)  
[3] [http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 1:20am UTC](https://www.ruby-forum.com/t/invoking-external-program/67439/3 "2006-09-05T01:20:36Z")

</div>

Hi

Comments made inline …
