I am look for a bash script that will check if a file has been modified in the last hour and email an alert if it has been modified. This script will be used in Solaris and Ubuntu. I am sure it's not hard, but I am not a Linux admin. Can someone please help?
-
Learn more about the `find(1)` command. Or perhaps pay a professional sysadmin or developper.... – Basile Starynkevitch Jan 04 '13 at 20:39
-
1One-liner cron job FTW! `* * * * * find /path/to/file -mmin -1 -exec mail -s 'Subject here' email@example.com <<< 'Message here' \;` – rinogo Sep 27 '18 at 13:55
2 Answers
How about this?
#!/bin/bash
[[ -z `find /home/spatel/ -mmin -60` ]]
if [ $? -eq 0 ]
then
echo -e "nothing has changed"
else
mail -s "file has been changed" spatel@example.com
fi
Put this script in hourly cron job
01 * * * * /path/to/myscript

- 16,544
- 29
- 93
- 149
linux supports the inotify
command. You use it to monitor file activity - file change, file creation whatever you want, whenever you want.
The find command given above will not work on out-of-the-box solaris. It is fine for linux. You have two options on Solaris:
go to www.sunfreeware.com and download gnu coreutils, which will install gnu find (the above version of find) in
/usr/local/bin
write a script that uses the touch command, wait more than 60 minutes then test the file, the only problem is this script runs in the background forever, you can avoid this if you know some perl to generate a timestring suitable for
touch -t [time string]
to create the file with a time one hour in the past This is the run forever version:while true
do touch dummy sleep 3615 # 1 hour 15 seconds
[ file_i_want_to_test -nt dummy ] && echo 'file blah changed' | mailx -s 'file changed' me@myco.com
done

- 16,005
- 2
- 34
- 51