-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_command.rb
More file actions
34 lines (28 loc) · 789 Bytes
/
run_command.rb
File metadata and controls
34 lines (28 loc) · 789 Bytes
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
#Function that accepts a bash command and timeout and returns an array with [exit_status, stdout, stderr]
require 'timeout'
def run_command(command, timeout)
last_exit_status = -1
# stdout, stderr pipes
rout, wout = IO.pipe
rerr, werr = IO.pipe
pid = Process.spawn(command, :out => wout, :err => werr)
begin
Timeout.timeout(timeout) do
_, status = Process.wait2(pid)
last_exit_status = status.exitstatus
end
rescue Timeout::Error
puts 'Timeout reached!'
last_exit_status = 1
Process.kill('TERM', pid)
end
# close write ends so we could read them
wout.close
werr.close
stdout = rout.readlines.join("\n")
stderr = rerr.readlines.join("\n")
# dispose the read ends of the pipes
rout.close
rerr.close
return [last_exit_status,stdout,stderr]
end