With Raspberry Pi 3B (hereinafter: Raspberry Pi), I would like to ** monitor the specified directory ** and run arbitrary processing when it is updated.
--Hook: Image is saved in A directory --What to do: Synchronize the state of A and B directories
Specifically, use ** inotify-tools ** to do something like the above.
Install with apt-get.
$ sudo apt-get install inotify-tools
ʻInotyfywait -e [Event] [Monitoring directory] `
Specify the specific event and the directory to be monitored with the inotfywait command.
This time, the moved_to (moved into the target directory)
event is monitored and processed.
inotify_single.sh
#!/bin/sh
inotifywait -m -e moved_to A | \
rsync -rv A B
I'm using the rsync command to synchronize A and B.
If you do not add the -m
option, inotifywait will end when the first event is issued, so it is added.
If this is left as it is, monitoring will end when a series of events are notified, so correct the process.
inotify_continuous.sh
#!/bin/sh
inotifywait -m -e moved_to A | \
while read _; do
rsync -rv A B
done
In this way, the while statement is used to continuously monitor.
[^ 1]: You can check the version of inotifywait with ʻinotifywait -hl`
-Monitor files and directories with inotify-tools
Recommended Posts