From: Joel VanderWerf Date: 2006-03-23T02:55:05+09:00 Subject: Re: Reading from standard error Thiago Arrais wrote: > Maybe this one has already been answered somewhere. If so, my > searching skills really suck. > > How can I start a sub-process from Ruby and read its standard error > and standard output stream separately? I have tried to do it with > IO.popen, but it only reads the output stream. The Process module > doesn't help either. > > Cheers, > > Thiago Arrais require 'open3' stdin, stdout, stderr = Open3.popen3( 'ruby -e "\$stdout.sync = true; while (s=gets); 5.times {\$stderr.puts(s.reverse); puts s}; end"') t1 = Thread.new do while (s=stderr.gets) puts "STDERR: #{s.inspect}" end puts "STDERR: DONE" end t2 = Thread.new do while (s=stdout.gets) puts "STDOUT: #{s.inspect}" end puts "STDOUT: DONE" end stdin.puts "foo" stdin.close [t1, t2].each do |t| t.join end The purpose of the $stdout.sync = true in the subprocess is so that you can read the subprocess's stdout as soon as it is written to. If all you care about is getting all the stdout _eventually_, you don't need it. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407