Description
inotify
allows you to add a watch descriptor to a file and send notifications to the system when an event affects that file. As a reminder, in the UNIX world, a file can represent a simple file, but also a directory, a device, a link, etc.
The main events that can be monitored are:
IN_ACCESS
: the file is read;IN_MODIFY
: the file is modified;IN_ATTRIB
: the file attributes are modified;IN_OPEN
: the file is opened;IN_CLOSE_WRITE
: the file is closed after being opened for writing;IN_CLOSE_NOWRITE
: the file is closed after being opened for reading;IN_MOVED_FROM
/IN_MOVED_TO
: the file has been moved or renamed;IN_DELETE_SELF
: the file has been deleted;IN_DELETE
: a file has been deleted in the watched directory;IN_CREATE
: a file has been created in the watched directory.
Incron
To install incron
:
sudo apt-get install incron
Once the installation is complete, you need to add the users authorized to use incron
in the /etc/incron.allow
file.
$ cat /etc/incron.allow
myuser
Usage
Add a file for each task/directory to watch in /etc/incron.d/
.
/my/watch/dir IN_CLOSE_WRITE php task.php --file $@/$#
Note: incron
does not handle recursion.
inotify-tools
Installation
sudo apt-get install inotify-tools
Description
inotify-tools
provides a way to call inotify
directly from the command line.
inotifywait
This command allows you to wait for an event before continuing execution. For example, to avoid executing a command that requires a file and falling into error:
inotifywait -e close_write /var/run/jboss.pid && supervision_jboss.sh
The -t
(timeout) option allows you to set a timeout in seconds, for example, to exit an error if an event takes too long to arrive:
backup.sh & ; inotifywait -e close_write -t 10000 backup_report || killall backup.sh
inotifywatch
inotifywatch
generates an activity report when events occur on the directories you are watching:
$ inotifywatch /var/cache/apt/archives &
[1] 18607
root@work:~# Establishing watches...
Finished establishing watches, now collecting statistics.
root@work:~# apt-get autoclean
root@work:~# kill %1
total access close_write close_nowrite open delete filename
1919 7 1 2 3 1906 /var/cache/apt/archives/
By default, inotifywatch
stops when it receives an interrupt signal. A timeout option (-t
) also exists to stop it after the specified number of seconds.
Watch activities in a user’s directory for one minute:
$ inotifywatch -r /home/user/ -t 60
Establishing watches...
Finished establishing watches, now collecting statistics.
total access modify close_write close_nowrite open moved_from moved_to create filename
16 3 3 2 0 2 2 2 2 /home/user/.mozilla/firefox/e3lq4lm3.default/
13 11 0 0 1 1 0 0 0 /home/user/.cache/myapp/mycache/
Set Up a Watcher Script
To conclude, here is how to set up a watcher script to run inotifywait
and send the output to a command. For example, you can create a watch.sh
file with this content:
#!/bin/bash
WATCH_PATH=/var/watch/dir/
PHP_BIN=/usr/bin/php
inotifywait -rme move,close_write,delete --format "%e %w%f" "$WATCH_PATH" | while read -r file; do
"$PHP_BIN" console cmd:opt "${file}"
done
Run the Watcher in the Background
chmod +x watch.sh
./watch.sh
[Ctrl+z]
bg
disown -h