Debian Codegraph @

btest-bg-wait

raw
1 #! /usr/bin/env bash
2 #
3 # Usage: btest-bg-wait [-k] <timeout>
4 #
5 # Waits until all of the background process spawned by btest-bg-run
6 # have finished, or the given timeout (in seconds) has been exceeded.
7 #
8 # If the timeout triggers, all remaining processed are killed. If -k
9 # is not given, this is considered an error and the script abort with
10 # error code 1. If -k is given, a timeout is not considered an error.
11 #
12 # Once all processes have finished (or were killed), the scripts
13 # merges their stdout and stderr. If one of them returned an error,
14 # this script does so as well
15
16 if [ "$1" == "-k" ]; then
17 timeout_ok=1
18 shift
19 else
20 timeout_ok=0
21 fi
22
23 if [ $# != 1 ]; then
24 echo "usage: $(basename "$0") [-k] <timeout>"
25 exit 1
26 fi
27
28 timeout=$1
29
30 procs=$(cat .bgprocs)
31
32 rm -f .timeout
33 touch .timeout
34
35 function check_procs {
36 for p in $procs; do
37 if [ ! -e "$p/.exitcode" ]; then
38 return 1
39 fi
40 done
41
42 # All done.
43 return 0
44 }
45
46 function kill_procs {
47 for p in $procs; do
48 if [ ! -e "$p/.exitcode" ]; then
49 kill -1 "$(cat "$p/.pid")" 2>/dev/null
50 cat "$p"/.cmdline >>.timeout
51 if [ "$1" == "timeout" ]; then
52 touch "$p/.timeout"
53 fi
54 fi
55 done
56
57 sleep 1
58
59 for p in $procs; do
60 if [ ! -e "$p/.exitcode" ]; then
61 kill -9 "$(cat "$p/.pid")" 2>/dev/null
62 sleep 1
63 fi
64 done
65 }
66
67 function collect_output {
68 rm -f .stdout .stderr
69
70 if [ $timeout_ok != 1 ] && [ -s .timeout ]; then
71 {
72 echo "The following processes did not terminate:"
73 echo
74
75 cat .timeout
76
77 echo
78 echo "-----------"
79 } >>.stderr
80 fi
81
82 for p in $procs; do
83 pid=$(cat "$p/.pid")
84 cmdline=$(cat "$p/.cmdline")
85
86 {
87 printf "<<< [%s] %s\\n" "$pid" "$cmdline"
88 cat "$p"/.stdout
89 echo ">>>"
90 } >>.stdout
91
92 {
93 printf "<<< [%s] %s\\n" "$pid" "$cmdline"
94 cat "$p"/.stderr
95 echo ">>>"
96 } >>.stderr
97 done
98 }
99
100 trap kill_procs EXIT
101
102 while true; do
103
104 if check_procs; then
105 # All done.
106 break
107 fi
108
109 timeout=$((timeout - 1))
110
111 if [ $timeout -le 0 ]; then
112 # Timeout exceeded.
113 kill_procs timeout
114
115 if [ $timeout_ok == 1 ]; then
116 # Just continue.
117 break
118 fi
119
120 # Exit with error.
121 collect_output
122 exit 1
123 fi
124
125 sleep 1
126 done
127
128 trap - EXIT
129
130 # All terminated either by themselves, or with a benign timeout.
131
132 collect_output
133
134 # See if any returned an error.
135 result=0
136 for p in $procs; do
137 if [ -e "$p/.timeout" ]; then
138 # we're here because timeouts are ok, so don't mind the exit code
139 # if we initiated killing the process due to timeout
140 continue
141 fi
142 rc=$(cat "$p/.exitcode")
143 pid=$(cat "$p/.pid")
144 cmdline=$(cat "$p/.cmdline")
145
146 if [ "$rc" != 0 ]; then
147 echo ">>> process $pid failed with exitcode $rc: $cmdline" >>.stderr
148 result=1
149 fi
150 done
151
152 exit $result