Skip to content

Instantly share code, notes, and snippets.

@kgilpin kgilpin/executor.rb
Created May 24, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.