summaryrefslogtreecommitdiff
path: root/ruby/timeout.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ruby/timeout.rb')
-rwxr-xr-xruby/timeout.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/ruby/timeout.rb b/ruby/timeout.rb
new file mode 100755
index 0000000..f5c1e7e
--- /dev/null
+++ b/ruby/timeout.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby
+
+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
+