From: Shea Martin Date: 2006-03-23T06:23:51+09:00 Subject: Re: Return code of process from IO Shea Martin wrote: > Shea Martin wrote: >> Farrel Lifson wrote: >>> Just solved it. Just run Process.wait after the call to the IO.popen >>> and $? gets updated. >> >> This doesn't work in this case: >> >> #!/usr/bin/ruby >> >> Thread.new{ >> IO.popen('ls non_existant_dir').each_line{|l| puts l} >> sleep 10 >> Process.wait >> puts "exit status is #{$?}, should be non-zero" >> } >> >> sleep 5 >> >> Thread.new{ >> IO.popen('dir').each_line{|l| puts l} >> Process.wait >> puts "exit status is #{$?}, should be 0" >> } >> >> sleep 15 >> >> exit 0 >> >> >> ~S > > This should work in a mt env. > > IO.popen('dir') { |p| > p.each_line{ |l| puts l } > puts "exitstatus is #{Process.waitpid(p.pid)}" > } that should be #{Process.waitpid2(p.pid)[1].exitstatus}