bash:directories:monitor_a_directory_for_changes:using_inotifywait
Differences
This shows you the differences between two versions of the page.
bash:directories:monitor_a_directory_for_changes:using_inotifywait [2022/06/13 09:15] – created peter | bash:directories:monitor_a_directory_for_changes:using_inotifywait [2022/06/13 09:17] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH - Directories - Monitor a directory for changes - Using inotifywait ====== | ====== BASH - Directories - Monitor a directory for changes - Using inotifywait ====== | ||
+ | ===== Install inotify-tools ===== | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt install inotify-tools | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Test inotifywait ===== | ||
+ | |||
+ | In a shell enter: | ||
+ | |||
+ | <code bash> | ||
+ | while true; do | ||
+ | res=`inotifywait -r thedirtowatch/ | ||
+ | echo " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | < | ||
+ | Setting up watches. | ||
+ | Watches established. | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * **< | ||
+ | * Symbolic links are not traversed. | ||
+ | |||
+ | |||
+ | See http:// | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | In a 2nd shell, go to that directory being watched. | ||
+ | |||
+ | <code bash> | ||
+ | cd thedirtowatch/ | ||
+ | touch test1 | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | <code bash> | ||
+ | RESULT=test1 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Only listen for certain events ===== | ||
+ | |||
+ | Change the inotifywait to the following: | ||
+ | |||
+ | <code bash> | ||
+ | while true; do | ||
+ | res=`inotifywait -q -e create, open, close, modify, moved_to, moved_from -r thedirtowatch/ | ||
+ | echo " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | Events include: | ||
+ | |||
+ | * **access**: A watched file or a file within a watched directory was read from. | ||
+ | * **modify**: A watched file or a file within a watched directory was written to. | ||
+ | * **attrib**: The metadata | ||
+ | * This includes timestamps, file permissions, | ||
+ | * **close_write**: | ||
+ | * This does not necessarily imply the file was written to. | ||
+ | * **close_nowrite**: | ||
+ | * **close**: | ||
+ | * Note that this is actually implemented simply by listening for both **close_write** and **close_nowrite**, | ||
+ | * **open**: | ||
+ | * **moved_to**: | ||
+ | * This event occurs even if the file is simply moved from and to the same directory. | ||
+ | * **moved_from**: | ||
+ | * This event occurs even if the file is simply moved from and to the same directory. | ||
+ | * **move**: | ||
+ | * Note that this is actually implemented simply by listening for both **moved_to** and **moved_from**, | ||
+ | * **move_self**: | ||
+ | * After this event, the file or directory is no longer being watched. | ||
+ | * **create**: | ||
+ | * **delete**: | ||
+ | * **delete_self**: | ||
+ | * After this event the file or directory is no longer being watched. | ||
+ | * Note that this event can occur even if it is not explicitly being listened for. | ||
+ | * **unmount**: | ||
+ | * After this event the file or directory is no longer being watched. | ||
+ | * Note that this event can occur even if it is not explicitly being listened to. | ||
+ | |||
+ | The **< | ||
+ | |||
+ | The **-q** option is to not show too much output. | ||
+ | |||
+ | See http:// | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using monitor mode ===== | ||
+ | |||
+ | Instead of running in a **while true; do** loop, an alternative is to use monitor mode: | ||
+ | |||
+ | <code bash> | ||
+ | inotifywait -m thedirtowatch/ | ||
+ | while read dir action file; do | ||
+ | echo "The file ' | ||
+ | # do something with the file. | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * **< | ||
+ | * The default behavior is to exit after the first event occurs. | ||
+ | |||
+ | See http:// | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using a BASH Script ===== | ||
+ | |||
+ | <file bash after-change.sh> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | # Monitor the input directory and if any change occurs run the provided command: | ||
+ | # | ||
+ | # Usage: | ||
+ | # | ||
+ | |||
+ | if [ $# -lt 3 ]; then | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | which inotifywait 2>&1 >/ | ||
+ | |||
+ | if [ " | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | dir=$1 | ||
+ | shift | ||
+ | cmd=$1 | ||
+ | shift | ||
+ | options=$* | ||
+ | |||
+ | inotifywait -q -r -m $dir $options | ||
+ | eval $cmd | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | http:// |
bash/directories/monitor_a_directory_for_changes/using_inotifywait.1655111714.txt.gz · Last modified: 2022/06/13 09:15 by peter