-
-
Save tamouse/6708213 to your computer and use it in GitHub Desktop.
This file contains 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
[21] pry(main)> kill_subprocess('yes',2,2) | |
Timed Out! | |
Tries: 1 | |
Timed Out! | |
Tries: 2 | |
=> "yes completed" | |
[22] pry(main)> |
This file contains 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' | |
def kill_subprocess(command,duration,retries) | |
output = '' | |
tries = 1 | |
begin | |
cmd = IO.popen(command, :err=>[:child, :out]) | |
Timeout.timeout(duration) do | |
while (line = cmd.gets) do | |
output << line | |
end | |
end | |
rescue Timeout::Error | |
$stdout.puts "Timed Out!" | |
Process.kill('KILL', cmd.pid) | |
$stdout.puts "Tries: #{tries}" | |
tries += 1 | |
retry unless tries > retries | |
ensure | |
Process.wait(cmd.pid) | |
end | |
"#{command} completed" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment