Help me keep writing code for others to use. Please consider making a donation of whatever you can afford if you use my scripts.
Monitor and log your website uptime with this 100% free web site monitoring software.
UPDATE: I now have written a GUI enabled version of Web Site Monitor, which has more features, and is much easier to use than this command line version.
UPDATE: I have now re-written, and renamed the PollWebSite.sh script to MonitorWebSite.sh, and it is available in the WebSiteMonitor.tar.gz file on the GUI Enabled version ofWeb Site Monitor page.
The newer version can still be used in command line mode, but gets installed into /usr/local/bin, so that it is not necessary to issue the absolute path to the file on the command line when it gets ran.
I was trying to find a program for Linux that would check my web sites every few minutes to see if they were up or not. I searched extensively with Google, and found many programs for Windows that do just that, but for Linux there seemed to only be full blown Network Monitoring software that required installing server software to use it.
I was puzzled, and asked on the Ubuntu Users mailing list if anyone knew of a free Web site monitoring tool for Linux systems, and they told me there weren't any, as it was too easy to write a script to do that. Then one guy responded that he had written such a script in Python that would do the trick.
He was too late. I had already discovered how indeed easy it was to write a bash script to check my web sites to see if they were up. I use a pair of scripts for this, one which monitors the web site, and another script is needed to stop the loop in the first one.
To use these scripts just download the zip file, then decompress the files into a folder off your home folder. Then issue the command "~/folderpath/PollWebSite.sh http://www.yoursite.com/filename.ext?query 300" from terminal to start monitoring your web site. [see usage notes below before trying this] To stop monitoring your web site, run "~/folderpath/StopPoll.sh" from Terminal.
#!/usr/bin/env bash # This file checks a valid URL on a web server to see if it can download the file specified. # If it is successful in downloading the file, it simply sleeps 5 minutes, then checks again. # If it is not successful it will pop up a warning that that web site is down. # It will continue to check the URL until it can once again successfully download the file, # then will pop up a dialog stating that the web site is back up again. # # USAGE: PollWebSite.sh http://www.rayslinks.com/monitor.txt 300 # # Where 300 is the sleep interval in seconds to wait between web site checks. # Replace http://www.rayslinks.com/monitor.txt with the url of an actual file on your web site. # function SubtractTimes { # Assign some variables StartTime="$1" EndTime="$2" # Extract hours, minues, and seconds from each time value # trim first two characters from DownTime Remainder=${StartTime#[0-9][0-9]} # extract first two characters of DownTime StartHours=${StartTime%$Remainder} # get rid of leading zeros StartHours=${StartHours#0*} # trim first three characters from DownTime Remainder=${StartTime#[0-9][0-9]?} # trim next two characters from front of Remainder Remainder=${Remainder#[0-9][0-9]} # trim first three characters from DownTime MinSec=${StartTime#[0-9][0-9]?} # extract minutes value from MinSec StartMinutes=${MinSec%$Remainder} # get rid of leading zeros StartMinutes=${StartMinutes#0*} # extract seconds from the end of DownTime StartSeconds=${StartTime#[0-9][0-9]*[0-9][0-9]?} # get rid of leading zeros StartSeconds=${StartSeconds#0*} # trim first two characters from UpTime Remainder=${EndTime#[0-9][0-9]} # extract first two characters of DownTime EndHours=${EndTime%$Remainder} # get rid of leading zeros EndHours=${EndHours#0*} # trim first three characters from DownTime Remainder=${EndTime#[0-9][0-9]?} # trim next two characters from front of Remainder Remainder=${Remainder#[0-9][0-9]} # trim first three characters from DownTime MinSec=${EndTime#[0-9][0-9]?} # extract minutes value from MinSec EndMinutes=${MinSec%$Remainder} # get rid of leading zeros EndMinutes=${EndMinutes#0*} # extract seconds from the end of DownTime EndSeconds=${EndTime#[0-9][0-9]*[0-9][0-9]?} # get rid of leading zeros EndSeconds=${EndSeconds#0*} # do the math to get the elapsed time if [ $EndHours -lt $StartHours ] || [ $EndMinutes -lt $StartMinutes ] then # suppose StartHours = 23 and EndHours = 00, so 24-23+00=1 Hours=$(( 24 - $StartHours + $EndHours )) else # suppose StartHours = 10 amd EndHours = 11, or they are equal... Hours=$(( $EndHours - $StartHours )) fi if [ $EndMinutes -lt $StartMinutes ] then Minutes=$(( 60 - $StartMinutes + $EndMinutes )) else Minutes=$(( $EndMinutes - $StartMinutes )) fi if [ $StartSeconds -gt $EndSeconds ] then Seconds=$(( 60 - $StartSeconds + $EndSeconds )) else Seconds=$(( $EndSeconds - $StartSeconds )) fi # return the value as a sentence echo "$Hours Hours, $Minutes Minutes, and $Seconds Seconds" } DirectoryName=`dirname $0` cd $DirectoryName if [ -d "$DirectoryName/logs" ] then # create the loop controller file. touch go.txt else mkdir "$DirectoryName/logs" # create the loop controller file. touch go.txt fi case "$1" in http://*) # pass in the url from the command line url=$1;; [0-9]*) echo "USAGE: PollWebSite.sh http://www.somesite.com/filename.ext 300 where 300 is interval in seconds." exit;; *) echo "USAGE: PollWebSite.sh http://www.somesite.com/filename.ext 300 \n where 300 is interval in seconds." exit;; esac # pass in the sleep interval Interval=$2 if [ "$Interval" == "" ] then Interval=300; # five minutes default setting fi # Add this script run's PID to URLsMonitored.txt for GUI wrapper script. touch URLsMonitored.txt echo $url $$ >>URLsMonitored.txt # generate a file name from the PID of this script as it is assigned when it runs. fname="$$.html" while [ -e go.txt ] do # get today's date for the daily log name. today=$(date +%d%m%Y) # create daily log file so wget can append to it touch "logs/$today.log" # see if we can download the file from the server # connect-timeout specifies in seconds how long wget will attempt to get the file before reporting failure. if wget --append-output="logs/$today.log" --connect-timeout=20 --output-document="$fname" "$url" 2>/dev/null # if wget downloaded the file, remove the file for the next loop then rm $fname # if the site was previously down, remove the lock file if [ -e messagedown$$.txt ] then rm messagedown$$.txt # and notify the user that the site is back up. xmessage -nearmouse "$url is up and running again!" & # Generate time stamp for daily log file UpTime=$(date +%H:%M:%S) # Calcualate how long the web site was down for. TimeDifference=`SubtractTimes $DownTime $UpTime` # Record that the web site is down in the daily log file. echo "$UpTime $url is up and running again after being down for $TimeDifference." >>"$DirectoryName/logs/$today.log" echo " " >>"$DirectoryName/logs/$today.log" fi else # download failed, if previous message box is already up don't open a new one. if [ -e messagedown$$.txt ] # append subsequent failures to daily log then echo "nothing" >null else # download failed for the first time. # pop up a message about the site being down. xmessage -nearmouse "$url is down!" & DownTime=$(date +%H:%M:%S) # Record that the web site is down in the daily log file. echo "$DownTime $url just went down." >>"$DirectoryName/logs/$today.log" echo " " >>"$DirectoryName/logs/$today.log" # prevent popping up the same message box over and over if site is still down touch messagedown$$.txt fi fi # wait for the next check time. sleep $Interval done # notify user that we've exited Web Monitoring xmessage -nearmouse "No longer monitoring $1" & exit
A daily log is kept of the download success, and failure messages from wget, and each day's file will have a name in the form DDMMYYYY.log. The log file automatically rolls over to a new one at midnight each day.
You can specify the sleep timeout between site checks on the command line. The sleep command uses seconds as a parameter, so 60 seconds is one minute, and 300 as I have it set for by default, is five minutes. To specify a different sleep timer interval, pass it as a second parameter to the PollWebSite.sh script after the url. Failure to pass the interval value on the command line will cause the script to default to 300 seconds.
Here is the code to the StopPoll.sh script which stops the PollWebSite.sh script from running. Note that if the PollWebSite.sh script is currently at the sleep command, it will not exit until the time specified runs out, and the script goes back to the top of the loop to check for the existence of go.txt. Running StopPoll.sh will stop all running instances of PollWebSite.sh.
Deleting the go.txt file from the Web Site Monitor folder will also stop all instances of the PollWebSite.sh script from running as soon as their sleep timers run down.
It is not necessary to run StopPoll.sh before shutting down your computer, as PollWebSite.sh will automatically be stopped by your system at shut down with no adverse affects.
#!/usr/bin/env bash cd ~/SiteCheck rm go.txt # clean up in case the site has been down if [ -e messagedown.txt ] then rm messagedown.txt; fi exit
/home/username/folderpath/PollWebSite.sh http://www.yourdomain.com/yourfile.ext?query 240
Help me keep writing code for others to use. Please consider making a donation of whatever you can afford if you use my scripts, even something as small as one dime would help a lot. Thanks.