blob: 03237fe364e8c2b7093571d35dc7cc4afbfd9640 (
plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#!/bin/bash
# delay starting after 2 failed runs
delay_after=2
# reset delay counter after 60 seconds
delay_reset_after=60
# delay for 30 seconds
delay=30
times_ran=0
cleanup() {
if [[ -n "$pid" ]]; then
echo "killed... $pid" >&2
kill "$pid"
fi
exit
}
trap cleanup 2 15
while :; do
case $1 in
--delay)
if [[ ! $2 ]]; then
echo "Missing argument to --delay" >&2
exit 1
fi
delay=$2
shift 2
continue
;;
--reset-after)
if [[ ! $2 ]]; then
echo "Missing argument to --reset-after" >&2
exit 1
fi
delay_reset_after=$2
shift 2
continue
;;
--delay-after)
if [[ ! $2 ]]; then
echo "Missing argument to --delay-after" >&2
exit 1
fi
delay_after=$2
shift 2
continue
;;
--help|-h)
echo "usage: ${0##*/} [options] [--] <command ...>"
echo ''
echo 'Options:'
echo ' --help, -h this help'
echo ' --delay <number> amount of delay after enoguh failed runs'
echo ' --delay-after <number> amount of runs after which we wait'
echo ' --reset-after <number> reset the failed counter after this time'
exit 0
;;
--)
shift
break
;;
*)
break
;;
esac
shift
done
command=("$@")
while :; do
times_ran=$((times_ran+1))
if [[ $times_ran -gt $delay_after ]]; then
echo "sleeping for $delay seconds after $((times_ran-1)) runs in $delay_reset_after seconds" >&2
sleep "$delay"
fi
time_started=$(date +%s)
echo -n "running... " >&2
"${command[@]}" &
pid=$!
echo PID: $pid >&2
wait $pid
pid=""
# reset counter if run long enough
if [[ $((time_started+delay_reset_after)) -lt $(date +%s) ]]; then
times_ran=0
fi
done
|