-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeout.sh
74 lines (59 loc) · 1.63 KB
/
timeout.sh
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#
# bashfoo timeout
#
#
#
# with_timeout SECONDS COMMAND...
#
with_timeout()
{
local timeout=$1
shift
local mark_timeout=.timeout_finished
local mark_cmd_finished=.cmd_finished
local result_file=.result
# remove temporaries
rm -rf $mark_timeout $mark_cmd_finished $result_file
# spawn command control subprocess ($cmd_pid)
(
exec "$@"
) &
local cmd_pid=$!
# spawn timeout control subprocess $(timeout_pid)
(
sleep $timeout
if [ ! -f "$mark_cmd_finished" ] ; then
log_error "timeout ${timeout}s reached for '$@', terminating job"
echo "timeout" >> "$result_file"
kill -TERM $cmd_pid
sleep 10
log_error "command '$@' still alive, KILL-ing it"
kill -KILL $cmd_pid
fi
touch "$mark_timeout"
) &
local timeout_pid=$!
# wait for command subprocess
wait $cmd_pid 2>/dev/null
# save the result
local command_result=$?
touch "$mark_cmd_finished"
log_debug "job done -> $command_result"
echo "$command_result" >> "$result_file"
if [ ! -f "$mark_timeout" ] ; then
log_debug "killing timeout job"
kill -TERM $timeout_pid 2>/dev/null
fi
# and for timeout subprocess
wait 2>/dev/null
result="$(cat "$result_file")"
# remove temporaries
rm -rf $mark_timeout $mark_cmd_finished $result_file
log_debug "done, result -> '$result'"
if [ "$result" = 0 ] ; then
return 0
else
return 1
fi
}
# jedit: :tabSize=8:indentSize=4:noTabs=true:mode=shell: