]> Sergey Matveev's repositories - sgmon.git/blob - run.sh
13ff28e8a32547c0165a149fc29532a18b11ce15
[sgmon.git] / run.sh
1 #!/bin/sh -e
2
3 export SGMONDIR="$(dirname "$(realpath -- "$0")")"
4 . "$SGMONDIR"/rc
5 [ -z "$1" ] && SGMONSRV="$(basename "$PWD")" || SGMONSRV="$1"
6 export SGMONSRV
7 export SGMONTMP=`mktemp -d -t sgmon`
8 trap "rm -fr $SGMONTMP" HUP PIPE INT QUIT TERM EXIT
9 rm -f state
10 ln -s $SGMONTMP state
11
12 while : ; do
13     [ -s max-attempts ] && read max_attempts < max-attempts || max_attempts=1
14     [ -s state/attempts ] && read attempts < state/attempts || attempts=0
15     if ./run >state/stdout 2>state/stderr ; then
16         if [ "$attempts" -ge "$max_attempts" ] ; then
17             "$SGMONDIR"/notify-ok.sh
18         fi
19         rm -f state/attempts
20         touch state/ok
21         echo OK
22     else
23         rm -f state/ok
24         attempts=$(( $attempts + 1 ))
25         echo $attempts > state/attempts
26         echo BAD ${attempts}/${max_attempts}
27         if [ $attempts -eq $max_attempts ] ; then
28             "$SGMONDIR"/notify-bad.sh
29         fi
30     fi
31     [ -s period ] && read period < period || period=$PERIOD_DEFAULT
32     sleep $period
33 done