Recently I needed to monitor a couple of websites and get instant notifications when ever something on them changed. First, I signed up at visualping.io, a service that monitors websites and sends an email when a change is detected. I liked their service, however, it isn’t free and I soon ran out of the 100 free credits new users receive. (Visualping.io now offers a freemium plan where users can monitor 2 jobs per days for free.)
Inspired by this blog post, I decided to create my own Bash script that monitors webpages and sends push notifications. Prowl is a great way to send custom notifications to an iPhone. The only fee is the $2.99 you pay for the app, after that you can send all of the notifications you want for free.
The script downloads the URL with curl, checks it against the previous download with diff and sends a notification if there are any changes. I use my Ubuntu server on Digital Ocean to run the script via cron, that way there is no need to have a computer on 24/7 or worry about slow speeds or outages.
You can check it out on GitHub, or follow these instructions:
- Create a directory for the script (I called mine website_diff/), and create two sub-folders:
- archive
- temp
- Copy the script below and paste it into a file called website_diff.sh, and make it executable with chmod +x website_diff.sh
- Install prowl’s dependencies, and prowl:
- sudo apt-get install libwww-perl -y
- sudo perl -MCPAN -e ‘install Bundle::LWP’ (accept the defaults)
- wget http://www.prowlapp.com/static/prowl.pl
- chmod +x prowl.pl
- Move prowl.pl to the website_diff/ directory
- Edit the website_diff.sh file and configure, by adding your prowl API key and the path to the script (including the website_diff directory).
- Add the urls you wish to monitor to website_diff.sh
- Create a cronjob to execute website_diff.sh at the desired fequency.
That’s it, you should receive notifications when you add a URL and whenever a change occurs. Since the prowl.pl script is executed with the -url argument, when you swipe the notifications you will be redirected to the URL.
#!/bin/bash ############################### ########## CONFIG ########## PROWL_API_KEY="" PATH_TO_FILES="" #Enter urls here, each URL in quotes on a new line URLS=( "http://www.example.com" "http://www.anotherexample.com" ) ########## END CONFIG ########## ################################ #Move to the files' path. This is to facilitate running this script in cron. cd $PATH_TO_FILES for URL in "${URLS[@]}" do #Make url into a valid filename (replaces chars in brackets with a dash) NEW_FILE="${URL//[:\/.?]/-}.new.html" OLD_FILE="${URL//[:\/.?]/-}.old.html" #Move the existing file, in preparation of a new file mv temp/$NEW_FILE temp/$OLD_FILE #Download the url curl "$URL" -L --compressed -s > temp/$NEW_FILE #Compare the two files looking for any differences if ! diff temp/$NEW_FILE temp/$OLD_FILE; then #If differences are found, send an alert via prowl. ./prowl.pl -apikey="$PROWL_API_KEY" -application="Application" -event="Website Changed" -notification="$URL Changed." -url="$URL" #Copy the new version of the webpage to the archive. cp temp/$NEW_FILE archive/$(date +%F-%T)-$NEW_FILE fi done