Previous: , Up: Configuration   [Contents][Index]


5.6 Watcher

The ‘watcher’ statement configures a single event watcher. A watcher can control several events in multiple pathnames. Any number of watcher statements is allowed in the configuration file, each of them declaring a separate watcher.

watcher {
    path pathname [recursive [level]];
    file regexp-list;
    event event-list;
    command command-line;
    user name;
    timeout number;
    environ { ... };
    option string-list;
}

Statements within a watcher block are:

Config: path pathname [recursive [number]]

Defines a pathname to watch. The pathname argument must be the name of a directory or file in the file system. If pathname refers to a directory, the watcher will watch events occurring for all files within that directory. If the optional recursive clause is specified, this directory will be watched recursively, i.e. when any subdirectory is created in it, direvent will set up a watcher for files in this subdirectory. This new watcher will be an exact copy of the parent watcher, excepting for the pathnames. The optional number parameter defines a cut-off nesting level for recursive watching. If supplied, the recursive behaviour will apply only to the directories that are nested below that level.

If pathname refers to a regular file, the changes to that file will be monitored. Obviously, in that case the ‘recursive’ keyword makes no sense. If present, it will be silently ignored.

If the pathname does not exist, GNU direvent will defer setting up the watcher until it is created. In order to do so, it will find the longest directory prefix that exists in the file system and will construct a sentinel watcher to monitor creation of the next directory component. When this component is created, the sentinel wakes up to set up a similar watcher for the next directory component. Once it is done, the sentinel removes itself. This process continues until the pathname is eventually created. When it happens, the last sentinel will activate the configured watcher.

These actions are performed in reverse order upon removal of pathname or any of its trailing directory components.

Any number of path statements can appear in a watcher block. At least one path must be defined.

Config: file regexp-list

Selects which files are eligible for monitoring. The argument is a list of globbing patterns (in the sense of see fnmatch in fnmatch(3)) or extended regular expressions (see Extended regular expressions in GNU sed) one of which must match the file name in order for the watcher to act on it. A ‘!’ in front of a pattern or regular expression indicates negation. Such construct matches if the file name doesn’t match the pattern. Regular expressions must be surrounded by a pair of slashes, optionally followed by the following flags:

b

Use basic regular expressions.

i

Enable case-insensitive matching.

For example:

file ("*.cfg", "/.*\\.jpg/i");

In this statement, the first string (‘*.cfg’) is treated as a shell globbing pattern. The second one is a case-sensitive extended regular expression.

Config: event string-list

Configures the filesystem events to watch for in the directories declared by the path statements. The argument is a list of event names. Both generic and system-dependent event names are allowed. Multiple event statements accumulate.

A missing event statement means “watch all events”.

For example:

event (open,delete);
Config: command string

Defines a command to execute on event. The string is a command line just as you would type it in sh. It may contain macro and environment variables (see variable expansion), which will be expanded prior to execution.

For example:

command "/bin/prog -event $genev_name -file $file";

By default, the command is executed directly via execve system call. If ‘shell’ option is set, the command is executed via the shell set in the SHELL environment variable.

See handler environment, for a detailed discussion of how the command is executed.

Config: user string

Run command as this user.

Config: timeout number

Terminate the command if it runs longer than number seconds. The default is 5 seconds.

Config: option string-list

A list of additional options. The following options are defined:

shell

Invoke the handler command via shell, as $SHELL -c "command". If this option is set, only macro variables are expanded in command. Environment variable references are left to be expanded by the shell. See variable expansion.

wait

Wait for the program to terminate before handling next event from the event queue. Normally the program runs asynchronously.

stdout

Capture the standard output of the command and redirect it to the syslog with the ‘LOG_INFO’ priority.

stderr

Capture the standard error of the command and redirect it to the syslog with the ‘LOG_ERR’ priority.

Config: environ { ... }

Modify the handler command environment. See environ, for a detailed discussion of configuration statements within the curly braces. This statement applies to the environment, modified by the global environ statement, if any.

For compatibility with earlier versions of the program, GNU direvent also supports a legacy syntax of the environ statement. It is described in environ legacy syntax.


Previous: , Up: Configuration   [Contents][Index]