]> Sergey Matveev's repositories - sgmon.git/blob - run.sh
Ability to have shebangless run-s
[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     [ -x run ] && cmd=./run || cmd="sh -e run"
16     if $cmd >state/stdout 2>state/stderr ; then
17         if [ "$attempts" -ge "$max_attempts" ] ; then
18             "$SGMONDIR"/notify-ok.sh
19         fi
20         rm -f state/attempts
21         touch state/ok
22         echo OK
23     else
24         rm -f state/ok
25         attempts=$(( $attempts + 1 ))
26         echo $attempts > state/attempts
27         echo BAD ${attempts}/${max_attempts}
28         if [ $attempts -eq $max_attempts ] ; then
29             "$SGMONDIR"/notify-bad.sh
30         fi
31     fi
32     [ -s period ] && read period < period || period=$PERIOD_DEFAULT
33     sleep $period
34 done