Last active
July 19, 2023 08:05
-
-
Save ikoba/451cfef3a64411cf79b8bcdf8dd86654 to your computer and use it in GitHub Desktop.
Execute external command and output STDOUT, STDERR in real time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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