Skip to content

Instantly share code, notes, and snippets.

@t-cyrill
Created September 12, 2014 02:26
Show Gist options
  • Save t-cyrill/ecf97f7b39796aa4edf1 to your computer and use it in GitHub Desktop.
Save t-cyrill/ecf97f7b39796aa4edf1 to your computer and use it in GitHub Desktop.
Waiting for other spawned process with timeout
require 'timeout'
require 'shellwords'
cmd = "foo"
out = "bar.out"
cmd = "#{cmd} -o #{Shellwords.escape(out)}"
timeout = 1
# puts cmd
pid = spawn(cmd, :pgroup => true, :in=>"/dev/null", :out => "/dev/null", :err => "/dev/null")
process = Process.detach(pid)
begin
Timeout.timeout(timeout) do
process.join
end
rescue Timeout::Error
Process.kill :TERM, -pid
end
if process.value.exitstatus > 0
# error trap
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment