summaryrefslogtreecommitdiffstats
path: root/sup
blob: d2d84ebc889145476e9cbb02c34af57a1b2549bb (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
#!/bin/sh

# 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

command="$*"
times_ran=0

cleanup() {
  if [ -n "$pid" ]; then
    echo "killed... $pid"
    kill $pid
  fi
  exit
}

trap cleanup 2 15

while :; do
  times_ran=$(($times_ran+1))
  if [ $times_ran -gt $delay_after ]; then
    echo "sleeping for after $(($times_ran-1)) runs in $delay_reset_after seconds"
    sleep $delay
  fi

  time_started=`date +%s`
  echo -n "running... "

  $command &
  pid=$!
  echo PID: $pid
  wait $pid
  pid=""

  # reset counter if run long enough
  if [ $(($time_started+$delay_reset_after)) -lt `date +%s` ]; then
    times_ran=0
  fi
done