Last active
August 29, 2015 14:04
-
-
Save nicoolas25/a644d8712927f3e62a23 to your computer and use it in GitHub Desktop.
Follow line by line the output of a subprocess.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'timeout' | |
cmd = [ | |
'sleep 3 ; echo "toto" ; sleep 3 ; echo "toto"', | |
'find /*', | |
'find /tmp' | |
] | |
begin | |
output, input = IO.pipe | |
pid = spawn(cmd[2], out: input) | |
loop do | |
# Do not wait more than 2 seconds for an input, the process could be exited | |
Timeout::timeout(2) do | |
# Read one line at a time continuously | |
loop do | |
chunck = output.readline | |
puts "Line received: #{chunck}" | |
end | |
end rescue nil | |
# Check if the process is still alive | |
break if Process.waitpid(pid, Process::WNOHANG) | |
end | |
# Read the rest of the output (just in case) | |
# You should also choose a better suited number than 100000 depending on your command | |
remaining_chunck = output.read_nonblock(100000) rescue nil | |
puts "Remaining lines: #{remaining_chunck}" if remaining_chunck | |
# Display a nice message to the user | |
exit_status = $? | |
puts "The command and was #{exit_status.success? ? '' : 'not '}a success" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment