August 20, 2008

Scheduling jobs based on filesystem activity with incron

Author: Shashank Sharma

There are numerous documents, tutorials and guides detailing the workings and usage of cron, the de facto tool for scheduling jobs on Linux. While traditional cron jobs are executed at set times, inotify cron, or incron, is a cron clone that watches the filesystem for specified changes and executes the relevant commands. You can set incron to monitor a particular file or directory for changes and schedule jobs for when those changes occur.

Fedora users can use yum to install incron with the yum install incron command. Once installed, you need to start the incron daemon before you can schedule jobs. The command, service incrond start, executed as root, will start the incron daemon on and the chkconfig incrond on command will configure it to be started at boot time.

incrontab, much like crontab, is the table manipulator used to schedule jobs. Each line in incrontab contains a filename, a comma-separated list of filesystem events to watch, and the command to be executed. You can schedule jobs based on file or directory changes. Use the incrontab -t command for a list of all the filesystem events that incron can monitor. Listed below is a list of these events along with explanation.

IN_ACCESS: Watched file is accessed
IN_MODIFY: Watched file is modified
IN_ATTRIB: Metadata changed (permissions, extended attributes, timestamps, etc.)
IN_CLOSE_WRITE: Closed a writable file
IN_CLOSE_NOWRITE: Closed a non-writable file
IN_OPEN: Opened a file
IN_MOVED_FROM: File moved out of directory being watched
IN_MOVED_TO: File moved into directory being watched
IN_CREATE: New file/directory created in the watched directory
IN_DELETE: File/directory deleted from directory being watched
IN_DELETE_SELF: Watched file/directory was deleted
IN_ALL_EVENTS: Watch all listed events
IN_DONT_FOLLOW: Don't follow sym link
IN_ONLYDIR: Watch path only if it's a directory
IN_MOVE_SELF: Watched file/directory was deleted

If you want the system to play an audio file whenever a new file/directory is created in your home directory, use this incrontab entry: /home/linuxlala IN_CREATE paplay /usr/share/sounds/pop.wav. The command /home/linuxlala IN_CREATE rm -rf $@/$# works similarly but its purpose is far more evil. With this entry in the incrontab file, any directory or file created in the user's home directory would be immediately deleted. The special symbols, or wildcards ($@/$#), convey the complete path of the newly created file to the rm -rf command.

These wildcards are useful when you have to pass the filename to a script or to the command that is to be executed. For example, if you want to run a script that will replace all curse words in the file on all files created in the /home/linuxlala/Documents directory, your incrontab entry would be: /home/linuxlala/Documents IN_CLOSE_WRITE /home/linuxlala/ $@/$#. Notice we've used IN_CLOSE_WRITE and not IN_CREATE for this rule. This is because only files will evoke IN_CLOSE_WRITE while even directories can evoke the IN_CREATE event.

Apart from the $@ and the $# wildcards, which respectively tell you the path of the watched file and the event-related filename, there are some other wildcards as well, such as $% and $&, which can be used to determine the textual or the numerical event flags.

Each time you update your incrontab file, run the incrontab -d command to reload the user table. If you don't do this, incron will continue to follow the previously defined rules.

For all its uses and advantages, there are still a few things that you can't do with incron. For instance, all my attempts to get incron to open a newly created file in Gedit failed. I used the entry: /home/linuxlala IN_CLOSE_WRITE gedit $@/$#. I couldn't get incron to launch any graphical application when a watched event occurred, either. I set up rules to launch Gedit, Akregator, gnome-terminal, etc. whenever a new file was created, a file was deleted or a file was moved, respectively, but none of the applications were launched.

Changing strategy, I set up a rule that said whenever a new directory was created in the home directory, incron would open the quotes.txt file in my home directory with Gedit: /home/linuxlala IN_CREATE gedit /home/linuxlala/quotes.txt. This didn't work. Using the complete path of the graphical application, /usr/bin/gedit, had no effect either. And yet, getting incron to execute scripts, convey filenames as arguments to commands, play audio and mp3 files with mplayer, etc., all worked effortlessly. I tried to get the developer to comment on why incron plays hard ball with graphical applications, but I didn't get a reply.

incron seems like a promising little utility, but it still has some issues such as being incompatible with graphical applications and not having recursive capabilities. incron does not recursively look through directories for rule-matching, but this feature has been on the TODO list for quite some time. For now, despite being a very clever concept, incron is on my list of tools that require a little more love from their developers if they wish to become mainstream.


  • Reviews
  • Shell & CLI
  • Linux
Click Here!