/usr/share/doc/ruby-open4/examples/timeout.rb is in ruby-open4 1.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | require 'open4'
def show_failure
fork{ yield }
Process.wait
puts
end
#
# command timeout
#
show_failure{
open4.spawn 'sleep 42', 'timeout' => 1
}
#
# stdin timeout
#
show_failure{
producer = 'ruby -e" STDOUT.sync = true; loop{sleep(rand+rand) and puts 42} " 2>/dev/null'
consumer = 'ruby -e" STDOUT.sync = true; STDIN.each{|line| puts line} "'
open4(producer) do |pid, i, o, e|
open4.spawn consumer, 0=>o, 1=>STDOUT, :stdin_timeout => 1.4
end
}
#
# stdout timeout (stderr is similar)
#
show_failure{
open4.spawn 'ruby -e" sleep 2 and puts 42 "', 'stdout_timeout' => 1
}
|