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
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/sensu/io.rb', line 9
def popen(command, mode='r', timeout=nil, &block)
block ||= Proc.new {}
begin
if RUBY_VERSION < '1.9.3'
child = ::IO.popen(command + ' 2>&1', mode)
block.call(child)
wait_on_process(child, false)
else
options = {
:err => [:child, :out]
}
case RUBY_PLATFORM
when /(ms|cyg|bcc)win|mingw|win32/
shell = ['cmd', '/c']
options[:new_pgroup] = true
else
shell = ['sh', '-c']
options[:pgroup] = true
end
child = ::IO.popen(shell + [command, options], mode)
if timeout
Timeout.timeout(timeout) do
block.call(child)
wait_on_process(child)
end
else
block.call(child)
wait_on_process(child)
end
end
rescue Timeout::Error
kill_process_group(child.pid)
wait_on_process_group(child.pid)
['Execution timed out', 2]
rescue => error
kill_process_group(child.pid)
wait_on_process_group(child.pid)
raise error
end
end
|