]> Sergey Matveev's repositories - sgmon.git/blob - run.sh
Missed togrep
[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 mkdir -p /tmp/sgmon
8 export SGMONTMP=`TMPDIR=/tmp/sgmon mktemp -d -t sgmon`
9 rm -f state
10 ln -s $SGMONTMP state
11 trap "rm -fr state $SGMONTMP" HUP PIPE INT QUIT TERM EXIT
12
13 while : ; do
14     [ -s max-attempts ] && read max_attempts < max-attempts || max_attempts=$ATTEMPTS_DEFAULT
15     [ -s state/attempts ] && read attempts < state/attempts || attempts=0
16     [ -x run ] && cmd=./run || cmd="sh -e run"
17     if $cmd >state/stdout 2>state/stderr ; then
18         if [ "$attempts" -ge "$max_attempts" ] ; then
19             "$SGMONDIR"/notify-ok.sh
20         fi
21         rm -f state/attempts
22         touch state/ok
23         echo OK
24     else
25         rm -f state/ok
26         attempts=$(( $attempts + 1 ))
27         echo $attempts > state/attempts
28         echo BAD ${attempts}/${max_attempts}
29         if [ $attempts -eq $max_attempts ] ; then
30             "$SGMONDIR"/notify-bad.sh
31         fi
32     fi
33     [ -s period ] && read period < period || period=$PERIOD_DEFAULT
34     sleep $period
35 done