Send E-Mail on File Change in monitored directory

对着背影说爱祢 提交于 2020-05-15 18:36:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!