Skip to content

Instantly share code, notes, and snippets.

@ikoba
Last active July 19, 2023 08:05
Show Gist options
  • Select an option

  • Save ikoba/451cfef3a64411cf79b8bcdf8dd86654 to your computer and use it in GitHub Desktop.

Select an option

Save ikoba/451cfef3a64411cf79b8bcdf8dd86654 to your computer and use it in GitHub Desktop.
Execute external command and output STDOUT, STDERR in real time.
require 'open3'
Open3.popen3('seq 100000') do |stdin, stdout, stderr, wait_thr|
stdin.close_write
loop do
IO.select([stdout, stderr]).flatten.compact.each do |io|
io.each do |line|
next if line.nil? || line.empty?
puts line
end
end
break if stdout.eof? && stderr.eof?
end
unless wait_thr.value.success?
puts "error occurred: status (#{wait_thr.value.exitstatus})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment