2

I want to send an e-mail notification to guys in our company if a file changed in their staff folder on the server. I have a script that works fine on sending an e-mail on every file change using inotifywait. What I would like to do is on multiple file uploads (lets say 10 jpg's are being uploaded to somebody's staff folder) to only send out one email.

This script sends an email on every file change:

inotifywait --recursive --exclude '.DS_Store' -e create -e moved_to -m /media/server/Staff/christoph |
    while read path action file ; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        sendEmail -f server@email.com -t user@gmail.com -s smtpout.secureserver.net:80 -xu user@email.com -xp password \
         -u ""The file $file appeared in your directory"" -m ""To view your file go to $path""
done

What is the smartest way to go about this? Does it make sense to have inotify wait for further input for lets say 2 mins?

BTW I'm using sendemail for this since port 25 is blocked by the ISP.

mrchrister
  • 49
  • 5

1 Answers1

2

I would likely do this by writing the modification notices to a file (if I was doing it I would probably use a SQLite database), then run a cron job every few minutes to check the database and send an aggregated email.

Another option would be to use inotifywait to trigger a script to watch that specific file, it would just loop checking the size/modified time of the file, then sleep for some period of time. Once the file stopped growing you the script would append the file info out to a message file. The cron job would send the message file (if it was not empty) then truncate the file. This would avoid the need to read and write data from a log file.

Andy C
  • 146
  • 4
  • Thanks Andy, I was thinking about creating a log file and sending this out. The problem is that a file transfer might take up to 20 minutes since we work in video production and the files are large. I wonder if I could grab the output from netatalk server to see if a file transfer is in progress? How could I prevent the script from sending multiple emails? – mrchrister Apr 15 '15 at 18:03
  • 1
    depending on how the file transfer works, you might change from looking at "create" to "close_write". This would work if the file transfer is done with through a single file open and close. Using a database for this, you could also add a flag for whether the email was sent yet, and check track the file-size & modification time between runs of the cron job, and only send the email once the size and modification time has stopped changing. – Andy C Apr 15 '15 at 18:25
  • Thanks again, Andy! I'm trying a log file now. close_write did weird things .. like posting about every file in the folder continuously so I guess this doesn't work in this case. Since database and sql is probably too far above my head I have the output written to a log file. What a good way to check for a new line in a log file and if it is different, send an email to a user? Would I use tail -n 1 and then an if statement? – mrchrister Apr 15 '15 at 19:10