Skip to content

Instantly share code, notes, and snippets.

@samiam2013
Created March 13, 2022 15:41
Show Gist options
  • Save samiam2013/7e3ee2b5c4eae09c6749d786eaef7c47 to your computer and use it in GitHub Desktop.
Save samiam2013/7e3ee2b5c4eae09c6749d786eaef7c47 to your computer and use it in GitHub Desktop.
code for checking the websites (specifically in apache) are running and restarting the server gracefully (so no users get kicked) after notifying an email address about it's down status
#!/bin/bash
#
# ADD SUBDOMAINS THAT NEED TO BE KEPT RUNNING AND WARNED ABOUT IN THIS LIST SEPARATED BY SPACES
#
sites_list=(localhost 127.0.0.1 example.com subdomain.example.com)
#
# ADD YOUR EMAIL HERE, MAKING SURE THAT `echo "message" | sendmail user@domain.tld`
# works for your user and domain name when called ffrom the command line
#
email_recipient=user@localhost
#
# SCRIPT MUST BE RUN AS ROOT FOR APACHECTL TO AFFECT PORTS < 1024
#
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
needs_restart=0
for site in "${sites_list[@]}"; do
#echo $site;
status=$(curl -s -o /dev/null -w '%{http_code}' $site);
if [ $status -ne "200" ]; then
needs_restart=1;
message="website '${site}' responded with NOT OK response code '${status}'";
echo $message | sendmail $email_recipient;
fi;
done;
if [ $needs_restart -eq 1 ]; then
echo "restaring apache gracefully";
apachectl graceful;
fi;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment