diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-14 16:46:27 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-14 16:46:27 +0200 |
commit | 26c490f404731d053a6205719b6246502c07b449 (patch) | |
tree | 3aa721098f1251dfbf2249ecd2736434c13b1d48 /ruby/spawn_with_timeout.rb |
init
Diffstat (limited to 'ruby/spawn_with_timeout.rb')
-rwxr-xr-x | ruby/spawn_with_timeout.rb | 17 |
1 files changed, 17 insertions, 0 deletions
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 + |