Skip to content

Instantly share code, notes, and snippets.

@adamwiggins
Created April 19, 2010 23:00
Show Gist options
  • Save adamwiggins/371776 to your computer and use it in GitHub Desktop.
Save adamwiggins/371776 to your computer and use it in GitHub Desktop.
# Using PTY instead of IO.popen, see answer:
# http://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby
def spawn_command(command)
begin
PTY.spawn(command) do |stdin, stdout, pid|
Process.wait(pid)
begin
stdin.each { |line| message " #{line}" }
rescue Errno::EIO
# child process stopped giving input
end
end
return $?.exitstatus
rescue PTY::ChildExited => e
return e.status
# child process exited
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment