Skip to content

Instantly share code, notes, and snippets.

@kgilpin
Created May 24, 2012 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgilpin/2781577 to your computer and use it in GitHub Desktop.
Save kgilpin/2781577 to your computer and use it in GitHub Desktop.
Command execution with realtime feedback
exit_code = Open3.popen3(env, cmd, opts) do |stdin, stdout, stderr, thread|
stdin.close
# Create a thread to read from each stream
threads = [[:stdout, stdout], [:stderr, stderr]].collect do |method, stream|
Thread.new do
until (line = stream.gets).nil?
callback.send(method, line)
end
end
end
pid = thread.pid
yield(callback, pid) if block_given?
# Don't exit until the external process is done
threads.collect(&:join)
thread.value
end.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment