Description

With inotify you could add a watch descriptor on a file, and send notifications to the system when an event affect the file. As a reminder, in the UNIX world, a file can represent a simple file as well as a directory, a device, a link, etc.

The main events that can be followed are :

IN_ACCESS : the file is read ;
IN_MODIFY : the file is modified ;
IN_ATTRIB : the file attributs are modified ;
IN_OPEN : the file is open ;
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 watch directory ;
IN_CREATE : a file has been created in the watch directory ;

Incron

$ apt-get install incron When install is completed, you need to insert users, which authorize to use incron, in the file /etc/incron.allow

$ 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 : No recursivity with incron

inotify-tools

Install inotify Tools

$ apt-get install inotify-tools

Description

inotify-tools give the way to call inotify directly from the command line.

inotifywait

With this command you can wait an event before to continue the execution. For example, to avoid executing a command which need a file and fall in 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 get out of error if an event is too long :

$ backup.sh & ; inotifywait -e close_write -t 10000 backup_report || killall backup.sh

inotifywatch

inotifywatch make an activity report when events occured on your watching directories :

$ 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, inotifywath stop when it receive an interruption signal. Also a timeout option (-t) exist to stop after the number of second specify. Watch activities in user directory during 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

And to conclude, set up a watcher script to run inotifywait and send the output to a command. For example, you can create a sample watch 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 file; do
  $PHP_BIN console cmd:opt "${file}"
done

Run the Watcher in the Background

$ chmod +x watch
$ ./watch
[Ctrl+z]
$ bg
$ disown -h