Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby timeouts and system commands

I have a ruby timeout that calls a system (bash) command like this..

Timeout::timeout(10) {
  `my_bash_command -c12 -o text.txt`
}

but I think that even if the ruby thread is interrupted, the actual command keeps running in the background.. is it normal? How can I kill it?

like image 790
luca Avatar asked Nov 28 '11 05:11

luca


2 Answers

I think you have to kill it manually:

require 'timeout'

puts 'starting process'
pid = Process.spawn('sleep 20')
begin
  Timeout.timeout(5) do
    puts 'waiting for the process to end'
    Process.wait(pid)
    puts 'process finished in time'
  end
rescue Timeout::Error
  puts 'process not finished in time, killing it'
  Process.kill('TERM', pid)
end
like image 162
Mladen Jablanović Avatar answered Nov 13 '22 04:11

Mladen Jablanović


in order to properly stop spawned process tree (not just the parent process) one should consider something like this:

def exec_with_timeout(cmd, timeout)
  pid = Process.spawn(cmd, {[:err,:out] => :close, :pgroup => true})
  begin
    Timeout.timeout(timeout) do
      Process.waitpid(pid, 0)
      $?.exitstatus == 0
    end
  rescue Timeout::Error
    Process.kill(15, -Process.getpgid(pid))
    false
  end
end

this also allows you to track process status

like image 11
shurikk Avatar answered Nov 13 '22 05:11

shurikk