summaryrefslogtreecommitdiff
path: root/ruby/spawn_with_timeout.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ruby/spawn_with_timeout.rb')
-rwxr-xr-xruby/spawn_with_timeout.rb17
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
+