make a system call and then kill it if the timeout is reached

Language: Ruby

require 'timeout'

class System
 def self.system_with_timeout(timeout, *args)
  if ( (pid = fork) == nil )
    #child process
    @@logger.debug(args.join(' '))
    exec(*args)
  else
    success = false
    #parent process
    begin
      #TODO if the process fails return false
      success = Timeout::timeout(timeout){ Process.waitpid(pid) }
    rescue Timeout::Error
      @@logger.error "***** Timeout error"
      Process.kill("HUP", pid)
      Process.detach(pid)
    end
    success
  end
 end
end
Reveal More
Added 7 months ago by Twitterprofilephoto_normal zh