Skip to content

Instantly share code, notes, and snippets.

@Wikinaut Wikinaut/pagewatcher.sh
Last active Jun 7, 2018

Embed
What would you like to do?
pagewatcher.sh - MySimplePageWatcher - Watch a webpage for changes
#!/bin/bash
# Page watcher Cronscript
# initial 201804xx
if [ $# -lt 1 ] ; then
echo "pagewatcher watches an url and sends an e-mail in case of changes."
echo
echo "usage: pagewatcher.sh url [additional-mail-recipients] | clean"
echo
exit
fi
if [ $1 = "clean" ]; then
rm /tmp/pagewatch-*
exit
fi
url=$1
sendmailonfirstrun="false"
mailto="root"
if [ "$2" != "" ]; then
mailto="root,$2"
fi
md5=$(echo -n "$url" | md5sum | awk '{ print $1 }')
page="/tmp/pagewatch-${md5}"
pagelast="${page}.last"
diff="${page}.diff"
mail="/usr/bin/mail"
wget $url -O "${page}" &> /dev/null
if [ ! -f "${pagelast}" ]; then
if [ $sendmailonfirstrun = "true" ]; then
touch "${pagelast}"
else
cp "${page}" "${pagelast}"
fi
fi
if [ -s "${page}" ]; then
# Are there differences to the last run?
diff -q "${pagelast}" "${page}" > /dev/null 2>&1
if [ $? -ne 0 ] ; then
echo -e "$url has changed.\n" > "${diff}"
diff --suppress-common-lines -u0 "${pagelast}" "${page}" >> "${diff}" 2>&1
$mail -s "$url has changed." $mailto < "${diff}"
cp "${page}" "${pagelast}"
rm "${diff}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.