2

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...

4
  • 1
    How do you define "multiple files are being detected at once"? I think you need to implement a kind of timeout to determine whether or not to run CURL on each update.
    – yaegashi
    Commented Jul 20, 2015 at 21:24
  • You'd probably be better off with close_write instead of create Commented Jul 20, 2015 at 22:24
  • Since you're using rsync, why does it matter if you run it multiple times in quick succession? Commented Jul 20, 2015 at 22:25
  • I probably should have explained further, but this is for a security camera system that creates multiple video files, and these are the files that are being detected/monitored and then uploaded via rsync to my server. @yaegashi Commented Jul 21, 2015 at 15:13

2 Answers 2

4

Running rsync on every update is also wasteful. The following script runs rsync and curl once when there's no activity for 0.1 second since the last event.

#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
while true; do
  T=''
  while read $T path action file; do
    echo "The file '$file' appeared in directory '$path' via '$action'"
    T='-t 0.1'
  done
  rsync -avz -e "ssh -p 221" /usr/lib/unifi-video/data/videos/ [email protected]:~/"$time_stamp"/ 
  curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected"
done
0
3

Have you looked at lsyncd? It's pretty much the same as what you have set up manually, except it runs as a proper service.

3
  • Oh yeah I forget about lsyncd which would be much easier and better solution, thanks for this post. Could you provide a config example to run a custom process (curl in OP) on each transfer with some updates aggregated?
    – yaegashi
    Commented Jul 22, 2015 at 1:05
  • I'm not familiar enough with it to give an example. I just set it up, and let it do its job. You'd probably want to ask a separate question... or even better, ask on an lsync board somewhere. The other option might be to have a different process watch the lsyncd log file, and notify based on that. Commented Jul 22, 2015 at 17:14
  • Actually... watching lsyncd's log file is probably the way to go... you'd just end up with the same problem if you tried to work a notification in to the lsyncd config. But if you had something that waited for changes on the lsyncd log file, and then waited until say, a minute since the last change to it, and then send out a curl notification, you'd probably end up with what you want. Commented Jul 22, 2015 at 17:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.