A little help, please, with this script:
#!/bin/bash
WATCH_DIR="/home/media/Movies"
# Comanda FileBot
FILEBOT_CMD="filebot -rename -r -non-strict "$WATCH_DIR" --db TheTVDB --format "{n}/Season {s}/{n} - {s00e00} - {t}" --action move --order Airdat"
# Monitorizeaza modificarile
inotifywait -m -r -e close_write "$WATCH_DIR" --format '%w%f' |
while read file; do
echo "New Change: $file"
eval $FILEBOT_CMD
done
This script detects if a file is opened to write (modify) and when a file is closed from writing (close_write), it is all good, it is working, but I want the script to be loaded only after all files finished, after no other event triggered for 5 seconds. Any solution to this?
So, if I copy 5 files, I don’t want the script run 5 times, instead, I want the script to wait till all the files has finished the copy process, then run only once. Detect if no event triggered for 5 seconds, then run the action filebot only once… This should happens no matter what size the files have.
Thank you in advance
2
Answers
I think that I have done the proper script for what I wanted to achieve...
This script shows me an echo after every trigger, then waits 20 seconds and if there is no other trigger, it will fire a final echo with an optional command...
If someone have an optinion about this script, how to optimize it more, or if there could be a different approach, a better one, please share it.
About the double trigger "issue" with inotifywait, I made more tests and if I monitor the folder with "inotifywait -m", for event "close_write", it is acting a bit strange. Lets say that I will copy 5 files (file 1, 2, 3, 4, 5). I will get the echo for every trigger of finished file, but after 12-15 seconds of the "file 1" trigger, it will show the same trigger again. For a better exlpication, i will show you a result echos by copying 5 files:
Any idea why inotifywait doubles the first trigger after 12-15 seconds?
Thank you for the help
One way would be to not use monitoring mode (
-m
) but instead letinotifywait
exit when a single event happens, then reinvokeinotifywait
with a 5 second timeout using the-t
option and check the exit status. It’ll exit with0
if an event happened and2
if it timed out (and1
for error). Your monitoring script could then just loop to repeat the procedure.Example: