问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/29657292/send-e-mail-on-file-change-in-monitored-directory