User Tools

Site Tools


bash:directories:monitor_a_directory_for_changes:using_incron

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bash:directories:monitor_a_directory_for_changes:using_incron [2022/06/13 09:14] – created peterbash:directories:monitor_a_directory_for_changes:using_incron [2022/06/13 09:18] (current) peter
Line 1: Line 1:
 ====== BASH - Directories - Monitor a directory for changes - Using incron ====== ====== BASH - Directories - Monitor a directory for changes - Using incron ======
 +
 +**incron** is like cron but instead of time-based events, the events are based on file notifications.
 +
 +----
 +
 +===== Install incron =====
 +
 +<code bash>
 +sudo apt install incron
 +</code>
 +
 +
 +----
 +
 +===== Change Permissions =====
 +
 +Add users to the **/etc/incron.allow** file who are allowed to use incron.
 +
 +<file bash /etc/incron.allow>
 +peter
 +</file>
 +
 +<WRAP info>
 +**NOTE:**  One user per line.
 +
 +**/etc/incron.deny** can also be used to specifically prevent usage of incron by a user.
 +</WRAP>
 +
 +----
 +
 +===== Monitor =====
 +
 +Edit the incron table:
 +
 +<code bash>
 +incrontab -e
 +</code>
 +
 +and populate with:
 +
 +<code>
 +/etc/apache2/apache2.conf IN_MODIFY /usr/sbin/service apache2 stop
 +
 +/home/peter/some_dir_to_watch IN_MODIFY echo "$$ $@ $# $% $&"
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  This monitors the directory for changes and will perform the echo passing along some parameters.
 +
 +The format is very picky; use spaces, not tabs!
 +
 +  * The format is **path mask command**.
 +
 +Parameters can include:
 +
 +  * **$$**:  Prints a dollar sign.
 +  * **$@**:  The watched filesystem path.
 +  * **$#**:  The event-related file name.
 +  * **$%**:  The event flags/time (textually).
 +  * **$&**:  The event flags.time (numerically).
 +
 +
 +Events to monitor include:
 +
 +  * **IN_ACCESS**:  If a file is accessed or read.
 +  * **IN_ATTRIB**:  If metadata of a file change (e.g. timestamps, permissions).
 +  * **IN_CLOSE_WRITE**:  If a file opened for writing is closed.
 +  * **IN_CLOSE_NOWRITE**:  If a file or directory not opened for writing is closed.
 +  * **IN_CREATE**:  If a file or directory is created in a watched directory.
 +  * **IN_DELETE**:  If a file or directory is deleted from a watched directory.
 +  * **IN_DELETE_SELF**:  If a watched file or directory is deleted (or moved to a different filesystem).
 +  * **IN_MODIFY**:  If a file was modified.
 +  * **IN_MOVE_SELF**:  If a watched file or directory is moved within the filesystem.
 +  * **IN_MOVED_FROM**:  If a file or directory is moved out of the watched directory.
 +  * **IN_MOVED_TO**:  A file or directory is moved to the watched directory.
 +  * **IN_OPEN**:  A watched file or directory is opened.
 +
 +</WRAP>
 +
 +<WRAP important>
 +**IMPORTANT:**  incron is not capable of watching sub-directories.
 +
 +Only files within the path will be monitored.
 +
 +If sub-directories need to be monitored, they must be given their own entry.
 +
 +</WRAP>
 +
 +----
 +
 +===== List the incron table =====
 +
 +<code bash>
 +incrontab -l
 +</code>
  
bash/directories/monitor_a_directory_for_changes/using_incron.1655111674.txt.gz · Last modified: 2022/06/13 09:14 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki