To explain a little, I am currently monitoring a folder for any changes and when changes are detected it simply uploads the detected files to my server via rsync. This is working without any issues :
#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
rsync -avz -e "ssh -p 221" /$path/$file [email protected]:~/"$time_stamp"/
done
I found most of that script here : Script to monitor folder for new files?
Question : I am trying to add the following CURL line to the script above, but as multiple files are being detected at once it is also executing the CURL line multiple times. I am attempting to find a method to prevent the CURL line from being executed more than once when multiple files are detected?
curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected";
I have tried adding it as a new line directly under the rsync command, as well as using && after the rsync command. Both methods executed the CURL command multiple times.
Example of what I tried :
#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
rsync -avz -e "ssh -p 221" /$path/$file [email protected]:~/"$time_stamp"/
curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected";
done
Example of the output :
The file 'test30' appeared in directory '/usr/lib/unifi-video/data/videos/' via 'CREATE'
sending incremental file list
sent 39 bytes received 11 bytes 20.00 bytes/sec
total size is 0 speedup is 0.00
{
"success": true
}
The file 'test31' appeared in directory '/usr/lib/unifi-video/data/videos/' via 'CREATE'
sending incremental file list
sent 39 bytes received 11 bytes 20.00 bytes/sec
total size is 0 speedup is 0.00
{
"success": true
}
The two "success" lines show the CURL command has been executed twice, after each detection and upload.
Please let me know if I forgot to include any information...
close_write
instead ofcreate
rsync
, why does it matter if you run it multiple times in quick succession?