From: Robert Klemme Date: 2010-09-23T00:33:36+09:00 Subject: Re: having problems with open4 and stuck forked processes On Wed, Sep 22, 2010 at 2:31 PM, Tim Uckun wrote: > I am running a batch process which uses the wkhtmltoimage-i386 binary > to make screenshots of urls.  Unfortunately this is in beta and it > frequently hangs up and takes up 100% of one of the CPUs on the > machine. > > I have the following code to try and detect the hung process and kill > it but it doesn't always work and I was wondering if anybody has a > better idea of how to do this.  When I run it by testing simple > commands like sleep it works perfectly. In production with this binary > it doesn't seem to always work. What do you mean by that? Goes the timeout undetected? Can't you kill the process? Are there any unexpected error messages / exceptions? > def Util.shell_with_timeout(cmd, seconds = 3600) >    #the default timeout is an hour. That's probably way too long > >    Timeout::timeout(seconds) { >      @pid, @stdin, @stdout, @stderr = Open4.popen4(cmd) >      ignored, @status = Process::waitpid2 @pid >      if @status.exitstatus != 0 >        raise "Exit Status not zero" >      end >    } > >    @stdout ? @stdout.read.strip : '' >  rescue Timeout::Error >    Process.detach @pid >    Process.kill 'SIGKILL', @pid >    raise "Process Timed out" >  rescue => e >    msg =  @stderr ?  @stderr.read.strip : '' >    msg +=   e.to_s >    raise "Error during execution of command #{cmd}\n #{msg}" >  end A frequent problem with #popen methods is to not read file descriptors which can make the client hang (i.e. if it writes more than fits into a pipe). That could be something to check since you are not reading any of the streams. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/