Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created September 26, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakekara/6e877a8b981c6cbc50f34f2c43b072e0 to your computer and use it in GitHub Desktop.
Save jakekara/6e877a8b981c6cbc50f34f2c43b072e0 to your computer and use it in GitHub Desktop.
Bash script to check if a site is online (via ping) every X seconds until it is back online.
#!/usr/bin/env sh
# nagsite.sh - ping a site until it comes back online
RET_VAL=1;
#
# checksite - Check if a site is online
# args - url to ping
# returns - no returns, but modifies RET_VAL
# to store return value of ping
function checksite(){
echo "Checking site $1"
ping -c 1 $1 > /dev/null
RET_VAL=$?
if [[ $RET_VAL = 0 ]]; then
say "$1 is online"
echo "$1 is online"
else
# say "$1 is offline"
echo "$1 is offline"
fi;
}
#
# nagsite - keep calling checksite until the
# site is back online
# args - a site to check
# - number of seconds to sleep between
# checks
# rets - nothing
#
function nagsite(){
while [[ $RET_VAL != 0 ]]; do
checksite $1;
sleep $2
done
}
# usage: nagsite.sh google.com 60
# the above checks google.com every 60 seconds)
nagsite $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment