Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script checks if a website is up/down.
#!/bin/bash
################################################################################
# This script checks if a website is up/down.
# If the website is DOWN the script:
# 1. will send an email to the admin
# 2. will try to restart Apache
#
# It can be added to crontab and it needs a public key authentication with SSH access.
# Admin email ####################################################################
email=your@email
# Name of the Website ###########################################################
name="website"
# Server of the Website ###########################################################
server=your.server.ext
# User for the server ###########################################################
user=username
# URL of the Website ############################################################
url="https://url.to.check"
# Path of the script ############################################################
path=/path/of/the/script
# Wait ############################################################
#
# Waits befor checking again, after the Apache restart
wait=60
#################################################################################
# Pinging the URL
# HTTP/2
curl -s --head --request GET $url | grep "HTTP/2 200" > /dev/null;
# HTTP/1.1
#curl -s --head --request GET $url | grep "200 OK" > /dev/null;
if [ "$?" -eq 0 ]
then
# Website is UP
echo "UP | `date`"
echo "UP | `date`" >> "$path/logs/$name-status.log"
else
# Website is DOWN
echo "DOWN | `date`" >> "$path/logs/$name-status.log"
mail -s "$url is DOWN - Restarting" $email <<< "$url is DOWN! Restarting..."
# It connects to the remote server using Public Key SSH Authentication
# and tries to restart Apache
# You'll need to set up SSH authentication first
ssh $user@$server '/usr/local/apache2/apachectl restart'
# Waits a bit
sleep $wait
# Launches itself again
/bin/bash $path/$name-monitor.sh
fi
exit
# EOF #########################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment