Register
It is currently Mon Sep 01, 2014 5:28 pm

Script to monitor a directory for changes, and notify me


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Jun 24, 2011 7:10 am   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
Someone asked me how to do this, so here you go. Schedule this to run once every 5 minutes (or whatever you set $MYFREQUENCY to) and it will email $MYEMAIL if it detects any changes or new files.

Code:
#!/bin/bash
# Description: This script monitors $MYDIR (checking once every $MYFREQUENCY minutes) for
# files that have recently been created or modified, and if it finds one, it sends a logger
# message to LOGHOST, and/or an email to $MYEMAIL.

MYDIR=/dir/to/monitor
MYEMAIL='username@domain.com'
MYFREQUENCY='-5'
MESSAGEFILE="message.txt"
MYDATE=$(date +%y-%m-%d)
MYTIME=$(date +%H:%M)
MAILER=$(which mutt)
MYNAME=$(hostname)
MYSUBJECT="Data Changed in $MYDIR directory on host ${MYNAME}"

if [[ ! -f ${MAILER} ]] ; then
echo "Program \"mutt\" is required, but is not available on this host. Please install mutt package and try again."
exit 1
fi

FILECOUNT="$(find ${MYDIR} -mmin ${MYFREQUENCY} -type f | wc -l)"

if [ ${FILECOUNT} -gt 0 ]
then
echo "Directory ${MYDIR} on host ${MYNAME} has been modified:" >> /tmp/${MESSAGEFILE}
echo "Filename : Timestamp : Size in bytes" >> /tmp/${MESSAGEFILE}
find $MYDIR -mmin ${MYFREQUENCY} -type f -printf '%p : %Ac : %s bytes *** ' >> /tmp/${MESSAGEFILE}
logger -p local0.info "CHANGE DETECTED: $(cat /tmp/${MESSAGEFILE})"
cat /tmp/${MESSAGEFILE} | mutt -s "CHANGED DETECTED in host ${MYNAME}" -a /tmp/${MESSAGEFILE} ${MYEMAIL}
/bin/rm -f /tmp/${MESSAGEFILE}
fi

exit 0


Top
 Profile WWW  
 PostPosted: Thu Aug 11, 2011 8:43 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I apologize for not actually properly examining your script, but I do know that incron is used for this type of monitoring.

http://inotify.aiken.cz


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP