Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Forked from mark-rushakoff/io_popen_example.rb
Last active August 29, 2015 14:08
Show Gist options
  • Save cheeyeo/644954cfaa88e1147f90 to your computer and use it in GitHub Desktop.
Save cheeyeo/644954cfaa88e1147f90 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
cmd = %q[echo '3...'; sleep 1;
echo '2...'; sleep 1;
echo '1...'; sleep 1;
echo 'Liftoff!']
puts '------ beginning command ------'
output_log = []
IO.popen(cmd).each do |line|
puts line
output_log << "[#{Time.now}] #{line}"
end.close # Without close, you won't be able to access $?
puts '------ done with command ------'
puts "The command's exit code was: #{$?.exitstatus}"
puts 'Here is the log:'
puts output_log.join('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment