# Capturing the output of an external program line by line

**URL:** <https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212>\
**Category:** Ruby\
**Created:** [October 14, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212 "2007-10-14T21:24:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [October 14, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212/1 "2007-10-14T21:24:30Z")

</div>

Hi,

I want to run an external program in ruby. The external program takes  
a long time to execute. While running, it gives the information on  
stdout about what it is doing. How can I run the external program  
inside ruby so that I can capture this information, and display it on  
stdout? I tried %x{…}, `...` and system("…"), but all of them  
give me a result AFTER the external program has finished executing,  
not during its execution.

Thanks,  
Aditya

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [October 14, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212/2 "2007-10-14T21:29:47Z")

</div>

Am Mon, 15 Oct 2007 04:23:41 +0900  
schrieb Aditya M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks,  
> Aditya

in,out = IO.popen($command)

in refers to standart input of the program  
out to the standart output

you can receive lines from the program using in.gets. Beware, “in” may  
be a reserved word in ruby.

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [October 14, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212/3 "2007-10-14T21:39:08Z")

</div>

Aditya M. wrote:

> Thanks,  
> Aditya

I’ve been using this:

```
IO.popen("#{cmd} 2>&1") do |f|
  while line = f.gets do
    # do whatever you want with line
  end
end

```

Note that this gets both stdout and stderr.

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [October 14, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212/4 "2007-10-14T21:51:11Z")

</div>

Tim H. wrote:

> Aditya M. wrote:
> 
> > Thanks,  
> > Aditya
> 
> I’ve been using this:
> 
> ```
> IO.popen("#{cmd} 2>&1") do |f|
> while line = f.gets do
> # do whatever you want with line
> end
> end
> 
> ```
> 
> Note that this gets both stdout and stderr.

Thank you Tim and Markus. Both your methods work perfectly. Since I just  
needed to capture the program’s output, I am using Tim’s method.

Thanks,  
Aditya

---

<div class="post-metadata">

**Author:** ![adityam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adityam](https://www.ruby-forum.com/u/adityam)\
**Post date:** [October 15, 2007, 1:29am UTC](https://www.ruby-forum.com/t/capturing-the-output-of-an-external-program-line-by-line/117212/5 "2007-10-15T01:29:02Z")

</div>

Markus S. wrote:

> in,out = IO.popen($command)
> 
> in refers to standart input of the program  
> out to the standart output

Are you absolutely sure IO.popen returns an array of two IOs?

mortee
