# Redirecting output ruby and from stdout results in blank file

**URL:** <https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430>\
**Category:** Ruby\
**Created:** [January 11, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430 "2010-01-11T21:40:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comp.lang.ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@comp.lang.ruby](https://www.ruby-forum.com/u/comp.lang.ruby)\
**Post date:** [January 11, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430/1 "2010-01-11T21:40:20Z")

</div>

Hi all, I’m executing a program from within my ruby script and  
attempting to capture the output of stderr and stdout. The relevant  
lines of code are below:

def create\_cmd(count)  
$webkit\_path + ’ ’ + $url + " 1\>std\_out.#{$options[:seed]}.#{$options  
[:prefix]}.#{$count} 2\>std\_err.#{$options[:seed]}.#{$options  
[:prefix]}.#{$count} "  
end

system(create\_cmd(0))

When I remove the redirection, I see all of the output that I expect.  
However, when I execute the above lines, only std\_err has content,  
while std\_out has nothing. The output being collected is standard  
printfs in a modified version of WebKit, if that’s relevant.

I am totally baffled how this is happening. I hope it’s something  
easy, my brain is waking up from vacation.

Be sure to let me know if I can provide any other relevant details to  
help diagnose this problem.

Thank you!

---

<div class="post-metadata">

**Author:** ![comp.lang.ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@comp.lang.ruby](https://www.ruby-forum.com/u/comp.lang.ruby)\
**Post date:** [January 11, 2010, 9:52pm UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430/2 "2010-01-11T21:52:02Z")

</div>

> When I remove the redirection, I see all of the output that I expect.  
> However, when I execute the above lines, only std\_err has content,  
> while std\_out has nothing. The output being collected is standard  
> printfs in a modified version of WebKit, if that’s relevant.

It’s possible that I’m misinterpreting what you want here, but you may  
want to look into the function popen3:

[http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html)

It let’s you specify what to do with the stdin, stdout, and stderr  
streams.

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![comp.lang.ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@comp.lang.ruby](https://www.ruby-forum.com/u/comp.lang.ruby)\
**Post date:** [January 11, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430/3 "2010-01-11T22:28:19Z")

</div>

El Lunes, 11 de Enero de 2010, Jonathan N.  
escribió:\> It let’s you specify what to do with the stdin, stdout, and stderr streams.  
I recommend popen4 as it also gets the correct exit status code of the  
executed command.

---

<div class="post-metadata">

**Author:** ![comp.lang.ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@comp.lang.ruby](https://www.ruby-forum.com/u/comp.lang.ruby)\
**Post date:** [January 11, 2010, 11:05pm UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430/4 "2010-01-11T23:05:47Z")

</div>

On Jan 11, 1:27 pm, Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks a lot for the suggestion. However, std\_error is redirecting  
fine and I have noticed that when I omit the redirection characters in  
the open command, the ouput is outputted to stdout, as I expect. I am  
happy to switch to popen if that’s the only solution – I just find  
this to be really weird behavior. Any guesses about why this happened?

---

<div class="post-metadata">

**Author:** ![comp.lang.ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@comp.lang.ruby](https://www.ruby-forum.com/u/comp.lang.ruby)\
**Post date:** [January 12, 2010, 12:11am UTC](https://www.ruby-forum.com/t/redirecting-output-ruby-and-from-stdout-results-in-blank-file/181430/5 "2010-01-12T00:11:19Z")

</div>

On Jan 11, 2010, at 5:05 PM, comp.lang.ruby wrote:

> Thanks a lot for the suggestion. However, std\_error is redirecting  
> fine and I have noticed that when I omit the redirection characters in  
> the open command, the ouput is outputted to stdout, as I expect. I am  
> happy to switch to popen if that’s the only solution – I just find  
> this to be really weird behavior. Any guesses about why this happened?

Is the standard output file being created and then is found to be empty  
or is it not being created at all?

If it isn’t getting created at all, then there is something wrong with  
your command line. If it is being created but is empty, then I would  
focus on the webkit program. Things to think about:

Take a look at the constructed command line. You didn’t show us that in  
your email.  
Is webkit designed to behave differently when writing to the terminal  
versus a file?  
Is the output being buffered when redirected to a file versus to the  
terminal?

Gary W.
