#!/bin/sh -e export SGMONDIR="$(dirname "$(realpath -- "$0")")" . "$SGMONDIR"/rc [ -z "$1" ] && SGMONSRV="$(basename "$PWD")" || SGMONSRV="$1" export SGMONSRV export SGMONTMP=`mktemp -d -t sgmon` rm -f state ln -s $SGMONTMP state trap "rm -fr state $SGMONTMP" HUP PIPE INT QUIT TERM EXIT while : ; do [ -s max-attempts ] && read max_attempts < max-attempts || max_attempts=1 [ -s state/attempts ] && read attempts < state/attempts || attempts=0 [ -x run ] && cmd=./run || cmd="sh -e run" if $cmd >state/stdout 2>state/stderr ; then if [ "$attempts" -ge "$max_attempts" ] ; then "$SGMONDIR"/notify-ok.sh fi rm -f state/attempts touch state/ok echo OK else rm -f state/ok attempts=$(( $attempts + 1 )) echo $attempts > state/attempts echo BAD ${attempts}/${max_attempts} if [ $attempts -eq $max_attempts ] ; then "$SGMONDIR"/notify-bad.sh fi fi [ -s period ] && read period < period || period=$PERIOD_DEFAULT sleep $period done