Triggering Commands On File/Directory Changes With Incron
Triggering Commands On File/Directory Changes With IncronVersion 1.0 This guide shows how you can install and use incron on a Debian Etch system. Incron is similar to cron, but instead of running commands based on time, it can trigger commands when file or directory events occur (e.g. a file modification, changes of permissions, etc.). This document comes without warranty of any kind! I do not issue any guarantee that this will work for you!
1 Installing IncronIncron is available in the etch-backports repository, so we have to add the following line to /etc/apt/sources.list: vi /etc/apt/sources.list
Let's import the backports.org archive key into apt... wget -O - http://backports.org/debian/archive.key | apt-key add - ... and run apt-get update The etch-backports repository is inactive by default, so to install incron from it, we must use the following command: apt-get -t etch-backports install incron
2 Using IncronIncron usage is very much like cron usage. You have the incrontab command that let's you list (-l), edit (-e), and remove (-r) incrontab entries. To learn more about it, see man incrontab There you also find the following section: If /etc/incron.allow exists only users listed here may use incron. Otherwise if /etc/incron.deny exists only users NOT listed here may use incron. If none of these files exists everyone is allowed to use incron. (Important note: This behavior is insecure and will be probably changed to be compatible with the style used by ISC Cron.) Location of these files can be changed in the configuration. This means if we want to use incrontab as root, we must either delete /etc/incron.allow (which is unsafe because then every system user can use incrontab)... rm -f /etc/incron.allow ... or add root to that file (recommended): vi /etc/incron.allow
Before you do this, you will get error messages like this one when trying to use incrontab: server1:~# incrontab -l Afterwards it works: server1:~# incrontab -l We can use incrontab -e to create incron jobs. Before we do this, we take a look at man 5 incrontab because it explains the format of the crontabs. Basically the format is as follows... <path> <mask> <command> ...where <path> can be a directory (meaning the directory and/or the files directly in that directory (not files in subdirectories of that directory!) are watched) or a file. <mask> can be one of the following: IN_ACCESS File was accessed (read) (*) When monitoring a directory, the events marked with an asterisk (*) above can occur for files in the directory, in which case the name field in the The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events. Two additional convenience symbols are IN_MOVE, which is a combination of IN_MOVED_FROM and IN_MOVED_TO, and IN_CLOSE which combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE. The following further symbols can be specified in the mask: IN_DONT_FOLLOW Don't dereference pathname if it is a symbolic link Additionaly, there is a symbol which doesn't appear in the inotify symbol set. It is IN_NO_LOOP. This symbol disables monitoring events until the current one is completely handled (until its child process exits). <command> is the command that should be run when the event occurs. The following wildards may be used inside the command specification: $$ dollar sign If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty. If you need the wildcards but are not sure what they translate to, you can create an incron job like this:
Then you create or modify a file in the /tmp directory and take a look at /var/log/syslog - this log shows when an incron job was triggered, if it succeeded or if there were errors, and what the actual command was that it executed (i.e., the wildcards are replaced with their real values). tail /var/log/syslog ... In this example I've created the file /tmp/huhu; as you see $@ translates to /tmp, $# to huhu, $% to IN_CREATE, and $& to 256. Now enough theory. Let's create our first incron jobs. I'd like to monitor the file /etc/apache2/apache2.conf and the directory /etc/apache2/vhosts/, and whenever there are changes, I want incron to restart Apache. This is how we do it: incrontab -e
That's it. For test purposes you can modify your Apache configuration and take a look at /var/log/syslog, and you should see that incron restarts Apache. To list all defined incron jobs, you can run incrontab -l server1:~# incrontab -l To delete all incron jobs of the current user, run incrontab -r server1:~# incrontab -r
3 Links
|



Recent comments
1 day 2 hours ago
1 day 8 hours ago
1 day 10 hours ago
1 day 13 hours ago
1 day 14 hours ago
1 day 20 hours ago
1 day 21 hours ago
1 day 22 hours ago
2 days 3 hours ago
2 days 3 hours ago