inotifywait (parte de inotify-tools ) é a ferramenta certa para alcançar seu objetivo , não importa que vários arquivos estejam sendo criados ao mesmo tempo, ele irá detectá-los.
Aqui está um exemplo de script:
#!/bin/sh
MONITORDIR="/path/to/the/dir/to/monitor/"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "[email protected]"
done
inotifywait will use these options.
-m to monitor the dir indefinitely, if you don't use this option, once it has detected a new file the script will end.
-r will monitor files recursively (if there are a lot of dirs/files it could take a while to detect the new created files)
-e create is the option to specify the event to monitor and in your case it should be create to take care about new files
--format '%w%f' will print out the file in the format /complete/path/file.name
"${MONITORDIR}" is the variable containing the path to monitor that we have defined before.
So in the event that a new file is created, inotifywait will detect it and will print the output (/complete/path/file.name) to the pipe and while will assign that output to variable NEWFILE.
Inside the while loop you will see a way to send a mail to your address using the mailx utility that should work fine with your local MTA (in your case, Postfix).
Se você quiser monitorar vários diretórios, o inotifywait não permite, mas você tem duas opções, crie um script para cada diretório para monitorar ou criar uma função dentro do script, algo assim:
#!/bin/sh
MONITORDIR1="/path/to/the/dir/to/monitor1/"
MONITORDIR2="/path/to/the/dir/to/monitor2/"
MONITORDIRX="/path/to/the/dir/to/monitorx/"
monitor() {
inotifywait -m -r -e create --format "%f" "$1" | while read NEWFILE
do
echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "[email protected]"
done
}
monitor "$MONITORDIR1" &
monitor "$MONITORDIR2" &
monitor "$MONITORDIRX" &