Bash Script to send email on file update

runt

2[H]4U
Joined
Mar 16, 2000
Messages
2,443
I have looked online and haven't found anythign yet, but what I need is a bash script that will send an email out to a pre-determined list of people when a file is modified. That way I don't need to tell my Android app testers that a new build is done (yes, I'm lazy that way). Doesn't seem like it should be to difficult to do, but of course it is difficult to me right now.
 
It is generally considered good etiquette to post the solution when you find it ( for future searches ).

For instance, I'm curious as to how you solved this problem. Did you manage to register some kind of handler with the file system, or does your script poll the file once every x seconds and check for an update file time?
 
Sorry, its a simple bash script that I found online. It runs once an hour and checks the md5sum of the file. If its different, it sends out the email. I setup a forward only email on my host and have that send out to my testers. You may or may not need to change the paths for mail, echo & md5sum. My web host requires full paths in a cronjob, so thats what I did.

Code:
#!/bin/sh 
# 
# 
# MD5FILE-parameter specifies where we want to save our md5print for 
# later use. 
MD5FILE=/path/to/file/.md5savefile 

# The FILE_TO_CHECK-parameter specifies the file we want to monitor 
# changes 
FILE_TO_CHECK=/path/to/file/filetocheck

if [ ! -f $FILE_TO_CHECK ] 
then 
/bin/echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK"
exit 1 
fi 

/bin/echo "Taking a print on $FILE_TO_CHECK with md5sum" 
MD5PRINT=`/usr/bin/md5sum $FILE_TO_CHECK | cut -d " " -f1` 

if [ -z $MD5PRINT ] 
then 
/bin echo "ERROR Recived an empty MD5PRINT thats not valid, aborting" 
exit 1 
else 
/bin echo "MD5PRINT we got was:$MD5PRINT" 
fi 

if [ -f $MD5FILE ] 
then 
/bin echo "Found an old savefile:$MD5FILE we trying to match prints" 
OLDMD5PRINT=`cat $MD5FILE` 

if [ -z $OLDMD5PRINT ] 
then 
/bin echo "Got an empty string from the oldfile, aborting" 
exit 1 
fi

if [ "$OLDMD5PRINT" = "$MD5PRINT" ] 
then 
/bin echo "New and old md5print are identical, the file hasnt been changed" 
else 
/bin echo "WARNING the old and new md5print doesnt match, the file has been changed" 
/bin echo "Whatver you want in your email here" | /usr/bin/mail -s "Subject here" [email protected]
fi 

fi 

/bin/echo "Saving to new md5print in logfile:$MD5FILE for later checks" 
/bin/echo $MD5PRINT > $MD5FILE 

if [ $? = 0 ] 
then 
/bin/echo "Wrote to file OK" 
else 
/bin/echo "Writing to file failed...why??" 
exit 1 
fi
 
A post-commit hook in version control would be a vastly superior method of solving this problem.
 
A post-commit hook in version control would be a vastly superior method of solving this problem.

Yes, it would be. But I use subversion provided by my web host and they limit what we can do via SVN. This does work for me.
 
I think I would modify that to simply check the mtime against a saved value. md5 hash can be expensive depending on the size of a file.
 
I think I would modify that to simply check the mtime against a saved value. md5 hash can be expensive depending on the size of a file.

The larger of the two files I'm checking right now is only ~462K and I honestly don't expect it to get above ~500K.
 
The larger of the two files I'm checking right now is only ~462K and I honestly don't expect it to get above ~500K.
Ya, I figured it wasn't anything serious, but for forms sake...

I'm anal-retentive on these things. Probably because I've put things like that in place then a year or so later the scope changes and I get bitten because of it. :D
 
Ya, I figured it wasn't anything serious, but for forms sake...

I'm anal-retentive on these things. Probably because I've put things like that in place then a year or so later the scope changes and I get bitten because of it. :D

LOL

If I knew how to re-write it the way you did I'd consider it.
 
LOL

If I knew how to re-write it the way you did I'd consider it.
Cron job:

Code:
find <path to file> -iname <filename> -mmin +59 -exec <do something magical here> \;

So if I were trying to find /root/test, it'd look like this

Code:
find /root -iname test -mmin +59 -exec /bin/mail -s "Subject is Awesomeness" "[email protected]" < /tmp/mailmsg  \;

I'm shooting from the hip here, but the mechanics should work even if I got some syntax wrong.
 
Back
Top