splitbrain.org

electronic brain surgery since 2001

Watcher, a Recursive incron Alternative

If you want something done regularily on your Linux machine, there is cron. It's a simple and reliable way to schedule tasks. But sometimes you want to run a job whenever a certain thing happens. If that thing is file related, incron is an excellent solution:

This program is an “inotify cron” system. You can use it a similar way as the regular cron. The difference is that the inotify cron handles filesystem events rather than time periods.

Unfortunately, incron has a limitiation: incron can't monitor directories recursively. That made it pretty useless for the application I had in mind and I started looking for alternatives.

One of them is called Watcher and was written by Greggory Hernandez. It's a Python daemon that basically does the same thing as incron, but can monitor directories recursively.

Greggory's orginal Watcher uses YAML files to configure monitoring directories and job setups. There's nothing wrong with YAML, but I'm not a big fan of adding dependencies when there are good enough alternatives in the core already.

So I forked his project at Github and made it use Python's default ini based config parser. I also added a few enhancements like standard argument parsing1).

Job setup is done in /etc/watcher.ini. Here is a real life example as I use it on my B3:

/etc/watcher.ini
; ----------------------
; General Settings
; ----------------------
[DEFAULT]

; where to store output
logfile=/var/log/watcher.log

; where to save the PID file
pidfile=/var/run/watcher.pid

 
; ----------------------
; Job Setups
; ----------------------
 
[job1]
; directory or file to watch.  Probably should be abs path.
watch=/home/storage

; list of events to watch for.
; supported events:
; 'access' - File was accessed (read) (*)
; 'attribute_change' - Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
; 'write_close' - File opened for writing was closed (*)
; 'nowrite_close' - File not opened for writing was closed (*)
; 'create' - File/directory created in watched directory (*)
; 'delete' - File/directory deleted from watched directory (*)
; 'self_delete' - Watched file/directory was itself deleted
; 'modify' - File was modified (*)
; 'self_move' - Watched file/directory was itself moved
; 'move_from' - File moved out of watched directory (*)
; 'move_to' - File moved into watched directory (*)
; 'open' - File was opened (*)
; 'all' - Any of the above events are fired
; 'move' - A combination of 'move_from' and 'move_to'
; 'close' - A combination of 'write_close' and 'nowrite_close'
;
; 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
; returned event data identifies the name of the file within the directory.
events=create

; if true, watcher will monitor directories recursively for changes
recursive=true

; the command to run. Can be any command. It's run as whatever user started watcher.
; The following wildards may be used inside command specification:
; $$ dollar sign
; $watched watched filesystem path (see above)
; $filename event-related file name
; $tflags event flags (textually)
; $nflags event flags (numerically)
command=/home/fixperm.sh $filename

The above defines a job to monitor the /home/storage directory recursively and call /home/fixperms.sh whenever a new file or directory is created.

Tags:
incron, cron, python, linux, software
Similar posts:
1)
using argparser – a core module in Python 2.7