Skip to content

Instantly share code, notes, and snippets.

@proglottis
Created September 12, 2013 05:38
Show Gist options
  • Save proglottis/6533389 to your computer and use it in GitHub Desktop.
Save proglottis/6533389 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def fork_with_output(&block)
std_pipe = IO.pipe
err_pipe = IO.pipe
fork do
$stdout.reopen(std_pipe[1])
$stderr.reopen(err_pipe[1])
block.call
std_pipe.each(&:close)
err_pipe.each(&:close)
exit!
end
std_pipe[1].close
err_pipe[1].close
[std_pipe[0], err_pipe[0]]
end
std, err = fork_with_output do
puts "start child"
$stderr.puts "EROOROOROOORORR"
sleep 1
puts "stop child"
end
$stdout.write std.read
$stderr.write err.read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment