Created
November 6, 2021 09:53
-
-
Save spchamp/92704f7040ff003ddc4e64d4c3af9724 to your computer and use it in GitHub Desktop.
Ruby I/O - capturing output of a subprocess on separate stdout and stderr descriptors
This file contains 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
## (No Warranty) | |
class SHFrob02 | |
def self.runfrob() | |
out_rd, out_wr = IO.pipe | |
err_rd, err_wr = IO.pipe | |
subpid = Process.spawn("ls -d /frob /etc", :in => :close, :out => out_wr, :err => err_wr ) | |
samepid, status = Process.wait2(subpid) | |
puts status | |
out = IO.new(out_rd.to_i, "r") | |
err = IO.new(err_rd.to_i, "r") | |
out_wr.close | |
err_wr.close | |
out.each_line do |txt| | |
puts "Out: #{txt}" | |
end | |
err.each_line do |txt| | |
puts "Err: #{txt}" | |
end | |
return status | |
ensure | |
## NB fails (EBADF) during close | |
# out.closed? || out.close | |
# err.closed? || err.close | |
out_wr.close | |
err_wr.close | |
out_rd.close | |
err_rd.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment