URL Monitoring With Bash
Several years ago, I was a poor college student in need of a car. Everytime I saw an ad for a car in my price range and called about it, I would be told it had already been sold. So I hacked together a simple Perl script that scraped local newpaper listings daily and emailed me a list of cars in my price range.
Fast forward a few years and I'm in the market for a new laptop. Lenovo's T61 is almost perfect, but I've gotten used to my current laptop's 1920x1200 screen. I refuse to move backwards. Rumor has it the T61p will be available in 1920x1200, so for the last couple of months I've been eagerly awaiting its announcement, but Lenovo appears to be in no hurry.
I'm tired of manually checking for new announcements. Thankfully, IBM periodically provides a file named tabook.pdf with a list of Thinkpad models. So I wrote the following script to periodically check for new versions and email me the latest as soon as it's detected:
#!/bin/bash
URL='http://buscaluz.org/robots.txt'
EMAIL="spam.spam.eggs.and.spam@buscaluz.org"
# Detect and send new copies of $URL to $EMAIL.
# $LOG timestamp is used to detect new $FILE.
FILE=$(basename "$URL")
LOG="${FILE}.log"
function log {
printf "%s: %s\n" $(date +%F) "$@" >> "$LOG"
}
if [ ! -f "$LOG" ]
then
log "Created log file."
touch -t 197001010000 "$LOG"
fi
wget -N "$URL"
if [ "$FILE" -nt "$LOG" ]
then
printf "A new copy of '$FILE' was discovered.\n\nurl: $URL\n" | \
mutt -s "New $FILE" -a "$FILE" "$EMAIL"
log "Sent new copy of '$FILE'."
fi
You'll notice I designed the script to be easily adapted. If you want to use it yourself, you'll probably also want to see the crontab entry that runs it:
15 0 * * * cd /home/sjansen/tabook && ./tabook.sh &>/dev/null