Skip to content

Instantly share code, notes, and snippets.

@fairchild
Last active March 21, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fairchild/1166f666f3baa0beec7e5f62142ac213 to your computer and use it in GitHub Desktop.
Save fairchild/1166f666f3baa0beec7e5f62142ac213 to your computer and use it in GitHub Desktop.
method to wrap a subprocess in a timeout
require 'timeout'
SUBPROCESS_TIMEOUT = 90
def timed_shell_command(cmd)
begin
Timeout.timeout(SUBPROCESS_TIMEOUT) do
subprocess = IO.popen(cmd)
Process.wait subprocess.pid
unless $?.success?
raise "shell subprocess failed with exit status #{$?.exitstatus}: #{cmd}"
end
end
rescue Timeout::Error
Process.kill 9, subprocess.pid
Process.wait subprocess.pid
raise "shell subprocess timedout: #{cmd}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment