From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- ruby/spawn_with_timeout.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 ruby/spawn_with_timeout.rb (limited to 'ruby/spawn_with_timeout.rb') diff --git a/ruby/spawn_with_timeout.rb b/ruby/spawn_with_timeout.rb new file mode 100755 index 0000000..a884673 --- /dev/null +++ b/ruby/spawn_with_timeout.rb @@ -0,0 +1,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 + -- cgit v1.2.3