7

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?

SueS
  • 123
  • 1
  • 2
  • 6

2 Answers2

14

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 
Satish
  • 16,544
  • 29
  • 93
  • 149
2

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:

  1. go to www.sunfreeware.com and download gnu coreutils, which will install gnu find (the above version of find) in /usr/local/bin

  2. 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
    
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51