summaryrefslogtreecommitdiff
path: root/ruby/spawn_with_timeout.rb
blob: a884673dee5f22072bb58c3d1b1dfcd160c2e0f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env ruby

require 'timeout'


cmd = "for i in {1..2000000}; do echo '123456789'; done"
pipe_in, pipe_out = IO.pipe
pid = Process.spawn(cmd, :out => pipe_out, :err => pipe_out)
begin
  Timeout.timeout(1) { Process.wait pid }
rescue Timeout::Error
  puts 'process not finished in time, killing it'
  Process.kill('TERM', pid)
end
pipe_out.close
puts pipe_in.read