GNU Direvent |
|
Directory event monitoring daemon |
Sergey Poznyakoff |
5.5 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 env-spec; option string-list; }
The 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 awatcher
block. At least onepath
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 namess are allowed. Multipleevent
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 variables (see macro 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/bin/sh
.See handler environment, for a detailed discussion of how the command is executed.
- 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 as
/bin/sh -c "command"
. - 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 env-spec
Modify command environment. By default the command inherits the environment of
direvent
augmented with the following variables:DIREVENT_SYSEV_CODE
The system-dependent event code (see the $sysev_code variable).
DIREVENT_SYSEV_NAME
The system-dependent event name or names (see the
$sysev_name
variable).DIREVENT_GENEV_CODE
The generic event code (see the
$genev_code
variable).DIREVENT_GENEV_NAME
The generic event name or names (see the
$genev_name
variable).DIREVENT_FILE
The name of the affected file relative to the current working directory (see the
$file
variable).
The
environ
statement allows for trimming the environment. Its argument is a list of environment modification directives. Before applying, each directive undergoes macro expansion (see macro expansion). The following directives are available:- ‘-’ (a single dash)
Clear the inherited environment, but retain the variables added by
direvent
itself. The removed environment variables can be selectively restored using the directives discussed below.If used, this must be the first directive in the list.
- ‘--’ (double-dash)
Clear the entire environment, including the variables added by
direvent
.If used, this must be the first directive in the list.
- -name
Unset the variable name.
- -name=val
Unset the environment variable name only if its value is val.
- name
Restore the environment variable name. This directive is useful after ‘-’ or ‘--’ to retain some variables from the environment.
- name=value
Define environment variable name to have given value.
- name+=value
Retain variable name and append value to its existing value. If no such variable is present in the environment, it is created and value is assigned to it. However, if value begins with a punctuation character, this character is removed from it before the assignment. This is convenient for using this construct with environment variables like
PATH
, e.g.:PATH+=:/sbin
In this example, if
PATH
exists, ‘:/sbin’ will be appended to it. Otherwise, it will be created and ‘/sbin’ will be assigned to it.- name=+value
Retain variable name and prepend value to its existing value. If no such variable is present in the environment, it is created and value is assigned to it. However, if value ends with a punctuation character, this character is removed from it before assignment.
Multiple
environ
statements accumulate.
This document was generated on July 13, 2019 using makeinfo.
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.