popen3(*cmd)
public
Open stdin, stdout, and stderr streams and start external executable.
Non-block form:
require 'open3'
[stdin, stdout, stderr] = Open3.popen3(cmd)
Block form:
require 'open3'
Open3.popen3(cmd) { |stdin, stdout, stderr| ... }
The parameter cmd is passed directly to Kernel#exec.
Show source
def popen3(*cmd)
pw = IO::pipe
pr = IO::pipe
pe = IO::pipe
pid = fork{
fork{
pw[1].close
STDIN.reopen(pw[0])
pw[0].close
pr[0].close
STDOUT.reopen(pr[1])
pr[1].close
pe[0].close
STDERR.reopen(pe[1])
pe[1].close
exec(*cmd)
}
exit!(0)
}
pw[0].close
pr[1].close
pe[1].close
Process.waitpid(pid)
pi = [pw[1], pr[0], pe[0]]
pw[1].sync = true
if defined? yield
begin
return yield(*pi)
ensure
pi.each{|p| p.close unless p.closed?}
end
end
pi
end