Tag: automation

  • Monitoring a Web Page for Changes

    Bash Script

    Today, I found myself needing a way to monitor a page. I didn’t need anything fancy, just something that would alert me if a page changed in any way. So, I set up a simple bash script and cron job to monitor the page. For me, this was a perfect solution. Since I’ve got a server running 24/7, it’s always able to monitor the page. This wouldn’t work quite as well from, say, a laptop, but a server or always-on desktop work perfectly. But in truth, all you really need is a system capable of running cron jobs. So, without further ado, whip open your favorite text editor and plug this in there:

    #!/bin/bash
    pageAddress="http://example.com/index.html"
    pageHashFile=/path/to/pageHash.txt
    
    newhash=$(curl "${pageAddress}" | md5sum | awk '{ print $1 }')
    oldhash=$(cat $pageHashFile)
    
    # Check the hashes, send an email if it's changed
    if [ $newhash != $oldhash ]; then
        echo "${pageAddress}" | mail -s "Page changed!" [email protected]
    
        # Only update the hash if the email was successfully sent.
        returnCode=$?
        if [[ $returnCode == 0 ]] ; then
            echo "${newhash}" > $pageHashFile
        fi
    fi
    

    Of course, you’ll need to change the page address, the path to where you want the hash put, and the email so that they meet your situation. Finally, just add the script to your crontab, and you’re good to go! I’ve got mine set to run every 10 minutes. To put it in your crontab, run crontab -e, and insert the following (adapt it as needed):

    */10 * * * * bash /path/to/script.sh
    

    It could be adapted to be more versatile and enable monitoring multiple pages, but since I just needed one (at least for now), this does the trick nicely.