Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Last active December 23, 2017 14:16
Show Gist options
  • Save trafficinc/3ef00843f99da61f2fee to your computer and use it in GitHub Desktop.
Save trafficinc/3ef00843f99da61f2fee to your computer and use it in GitHub Desktop.
Bash webpage checker, put in cronjob to check if website is up or not
#!/bin/bash
# WebPage checker
# ping 4 times in 5 seconds to check site uptime
# change webpage & EMAIL vars
badpings=()
str="unknown host"
webpage="www.yoursite.com"
EMAIL="youremail@gmail.com"
for ((i=0; i<3; i++))
do
ping_res=$(ping -c 1 $webpage 2>&1);
if [[ $ping_res == *"$str"* ]]
then
badpings+=(1)
fi
sleep 5
done
tot=0
for i in ${badpings[@]}; do
let tot+=$i
done
if [ "$tot" -ge "3" ]
then
echo "The site $webpage may be down and not responding" | mail -s "$webpage (http) failed" $EMAIL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment