Skip to content

Instantly share code, notes, and snippets.

@mikl0s
Created May 16, 2018 21:42
Show Gist options
  • Save mikl0s/034f90a1ff3448c2be39a7e24e3320c5 to your computer and use it in GitHub Desktop.
Save mikl0s/034f90a1ff3448c2be39a7e24e3320c5 to your computer and use it in GitHub Desktop.
Simple script to check if a webservice is running and restarts a service if it is not with logging
#!/usr/local/bin/bash
# Simple script to check if a webservice is running and restarts a service if it is not.
# Logfile that only grows upon status change.
# PATH added to avoid cron issues.
#
# mikl0s @ github.com 2018 - Written for FreeBSD/FreeNAS
#
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin"
URL="http://192.168.1.13:7878/"
SERVICE="radarr"
LOGFILE="/var/log/radarr_check.log"
DATE=`date "+%F %T"`
function check {
if [ $CURL -eq 0 ]
then
if [ $LAST -eq 0 ]
then
echo $DATE - $SERVICE running - Curl exit code: $CURL >> $LOGFILE
fi
exit 0
fi
if [ $CURL -eq 7 ]
then
echo $DATE - $SERVICE down - restarting >> $LOGFILE
service $SERVICE restart > /dev/null 2>&1
exit 0
fi
}
curl -s -o "/dev/null" $URL
CURL=$?
LAST=`tail -1 $LOGFILE|grep running|wc -l`
check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment