#!/bin/bash
#
# watchdog
#
# Run as a cron job to keep an eye on what_to_monitor which should always
# be running. Restart what_to_monitor and send notification as needed.
#
# This needs to be run as root or a user that can start system services.
#

NAME=OnBoardGpioServer
START="systemctl restart gpio-server"
STOP="systemctl stop gpio-server"
GREP=$(which grep)
PS=$(which ps)
NOP=$(which true)
DATE=$(which date)
LOGGER=$(which logger)

$PS -ef | $GREP -v grep | $GREP OnBoardGpioServer >/dev/null 2>&1
case "$?" in
 0)
 # It is running in this case so we do nothing.
 $NOP
 ;;
 1)
 $LOGGER "$NAME is NOT RUNNING. Starting $NAME ...."
 $START 2>&1 >/dev/null &
 $LOGGER "$NAME was not running and was started on `$DATE`"
 ;;
esac
