 <p>I've had some success in solving this problem of mine. Here are the details, with some explanations, in case anyone having a similar problem finds this page. But if you don't care for details, <strong>here's the short answer</strong>:</p> <p>Use PTY.spawn in the following manner (with your own command of course):</p> <pre><code>require 'pty' cmd = "blender -b mball.blend -o //renders/ -F JPEG -x 1 -f 1" begin PTY.spawn( cmd ) do |stdout, stdin, pid| begin # Do stuff with the output here. Just printing to show it works stdout.each { |line| print line } rescue Errno::EIO puts "Errno:EIO error, but this probably just means " + "that the process has finished giving output" end end rescue PTY::ChildExited puts "The child process exited!" end </code></pre> <p>And <strong>here's the long answer</strong>, with way too many details:</p> <p>The real issue seems to be that if a process doesn't explicitly flush its stdout, then anything written to stdout is buffered rather than actually sent, until the process is done, so as to minimize IO (this is <em>apparently</em> an implementation detail of many C libraries, made so that throughput is maximized through less frequent IO). If you can easily modify the process so that it flushes stdout regularly, then that would be your solution. In my case, it was blender, so a bit intimidating for a complete noob such as myself to modify the source. </p> <p>But when you run these processes from the shell, they display stdout to the shell in real-time, and the stdout doesn't seem to be buffered. It's only buffered when called from another process I believe, but if a shell is being dealt with, the stdout is seen in real time, unbuffered.</p> <p>This behavior can even be observed with a ruby process as the child process whose output must be collected in real time. Just create a script, random.rb, with the following line:</p> <pre><code>5.times { |i| sleep( 3*rand ); puts "#{i}" } </code></pre> <p>Then a ruby script to call it and return its output:</p> <pre><code>IO.popen( "ruby random.rb") do |random| random.each { |line| puts line } end end </code></pre> <p>You'll see that you don't get the result in real-time as you might expect, but all at once afterwards. STDOUT is being buffered, even though if you run random.rb yourself, it isn't buffered. This can be solved by adding a <code>STDOUT.flush</code> statement inside the block in random.rb. But if you can't change the source, you have to work around this. You can't flush it from outside the process.</p> <p>If the subprocess can print to shell in real-time, then there must be a way to capture this with Ruby in real-time as well. And there is. You have to use the PTY module, included in ruby core I believe (1.8.6 anyways). Sad thing is that it's not documented. But I found some examples of use fortunately.</p> <p>First, to explain what PTY is, it stands for <a href="http://en.wikipedia.org/wiki/Pseudo_terminal" rel="nofollow noreferrer">pseudo terminal</a>. Basically, it allows the ruby script to present itself to the subprocess as if it's a real user who has just typed the command into a shell. So any altered behavior that occurs only when a user has started the process through a shell (such as the STDOUT not being buffered, in this case) will occur. Concealing the fact that another process has started this process allows you to collect the STDOUT in real-time, as it isn't being buffered.</p> <p>To make this work with the random.rb script as the child, try the following code:</p> <pre><code>require 'pty' begin PTY.spawn( "ruby random.rb" ) do |stdout, stdin, pid| begin stdout.each { |line| print line } rescue Errno::EIO end end rescue PTY::ChildExited puts "The child process exited!" end </code></pre>