Created
September 26, 2018 18:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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