Skip to content

Instantly share code, notes, and snippets.

@andyj
Last active April 12, 2023 11:15
Show Gist options
  • Save andyj/b9e401a8f7bc477ac7c0405a69bb2574 to your computer and use it in GitHub Desktop.
Save andyj/b9e401a8f7bc477ac7c0405a69bb2574 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is provided "as is" and without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the script or the use or other dealings in the script. Use at your own risk.
# This is a bash script that checks the HTTP response of a list of top-level domain names
# every 1 minute. If a domain returns an HTTP status code other than 200 OK, the script
# sends a callback to a specified URL with the name of the domain that failed.
#
# The list of domains and callback URL can be edited
#
# You could also use CRON to ensure this file is alway running e.g.
# */5 * * * * /your/path/uptime_check.sh >/dev/null 2>&1
#
# Don't forget to run chmod +x uptime_check.sh to make sure the script is executable
# SCRIPT VARIABLES
# List of top-level domain names to check
domains=("www1.example.com" "www2.example.com" "www3.example.com")
# URL to make a callback in case of failed response
callback_url="https://rest.example.com/servicecheck"
# LOCK FILE path to ensure this is only running once
LOCK_FILE="/tmp/uptime_check.lock"
# Check if lock file exists
if [ -e "$LOCK_FILE" ]; then
echo "Script is already running. Exiting."
exit 1
else
# Create lock file
touch "$LOCK_FILE"
while true; do
for domain in "${domains[@]}"; do
if curl -s -m 30 -I "https://$domain" | grep "HTTP/.* 200" > /dev/null; then
# Uncomment if you want to run locally and drive yourself insane
# or simply change "say" to "echo"
# say "Success: $domain returned 200 OK"
else
# Uncomment if you want to run locally and drive yourself insane
# or simply change "say" to "echo"
# say "Error: $domain did not return 200 OK"
# Make the callback to your alert service
curl -X POST -d "domain=$domain" "$callback_url"
fi
done
sleep 60 # Wait for 1 minute before checking again
done
# Remove lock file
rm "$LOCK_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment