This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Reviews

Scheduling jobs based on filesystem activity with incron

By Shashank Sharma on August 20, 2008 (7:00:00 PM)

Share    Print    Comments   

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_CLOSE: Watch both IN_CLOSE_WRITE and IN_CLOSE_NOWRITE IN_MOVE: Watch both IN_MOVED_FROM and IN_MOVED_TO 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/replace_curse_words.sh $@/$#. 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.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Scheduling jobs based on filesystem activity with incron

Note: Comments are owned by the poster. We are not responsible for their content.

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 170.160.9.3] on August 20, 2008 08:01 PM
Probably dont have permission to use the X display (what user does incron run as?).....
That is where I would look first.....

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 83.32.130.122] on August 20, 2008 08:51 PM
So, I see you get to act on attributes. How about moving and compressing a log file when it hits a 100MB mark for example?

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 66.192.64.73] on August 20, 2008 08:58 PM
Eh, who cares about opening graphical apps. It's a boon for any serious sysadmin who needs to deal with file+event based transaction stuff. It's nice to be able to, say, move a file out of the way immediately after a ftp user's done uploading it without a bunch of ugly calls to lsof.

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 86.67.60.26] on August 20, 2008 11:13 PM
Anonymous [ip: 170.160.9.3] is probably right.
Have you tried
code : xhost + local:incron_user_name
in your normal user shell ?

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 207.138.32.130] on August 20, 2008 11:17 PM
Don't forget to set $DISPLAY as well.

#

Re: Scheduling jobs based on filesystem activity with incron

Posted by: Fletch on August 21, 2008 03:24 AM
Probably wouldn't fool with xhost and the display variables as that opens your computer to allow any other user on the system to open things to your display while you are using it, and really, shouldn't be needed.

So if incron runs as root, root doesn't have access to your display. Is incron allowing you to create your own cron file? If not, can you not make your job run like su - <your username> -c "command to be invoked" instead? I think that might pick up your environment so that it would have permission to update your display. Don't hate xauth. It's there for a reason.

Haz
[Modified by: Fletch on August 20, 2008 09:34 PM]

#

cron can't do this either

Posted by: Anonymous [ip: 71.142.66.74] on August 22, 2008 06:56 AM
IIRC, cron can't run graphical applications either. I think it has something to do with the lack of "controlling terminal".
If you do a `ps ax' you will see
3112 ? S 0:00 /usr/sbin/crond
but
3760 pts/0 Rs 0:00 -/bin/bash
The question mark shows that the crond's stdin/stdout/stderr are not associated with an interactive terminal and the processes it spawns can't inherit such a terminal.
(This is merely a guess)

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 220.233.218.81] on August 22, 2008 04:57 PM
Hi Shashank,

Try the following:

/home/linuxlala IN_CLOSE_WRITE DISPLAY=:0 gedit $@/$#

- Venkatesh Kanchan

#

Launching a Graphical App

Posted by: Anonymous [ip: 204.8.156.142] on August 23, 2008 04:29 AM
Probably the best way to launch a graphical app would be something like:

/home/linuxlala IN_CLOSE_WRITE su linuxlala -c "/home/linuxlala/bin/script $@ $#"

Where /home/linuxlala/bin/script does some sanity checks such as making sure the user linuxlala is actually using the GUI and then setting $DISPLAY appropriately and launching the desired app. Note that I passed $@ and $# as parameters for use by the script. Also, the script is being run by user linuxlala so I am not opening a security hole.

If you just need something quick-n-dirty, set $DISPLAY and su to the appropriate user and call the app.

@Anonymous [ip: 71.142.66.74]

I just tried

35 * * * * root DISPLAY=:1 su username -c /usr/X11R6/bin/xterm

in /etc/crontab (where user username was using :1) and it worked just fine.

#

No answer because it is stated in the manpage

Posted by: Anonymous [ip: 141.35.141.150] on August 25, 2008 09:46 AM
No offence, but like everybody else you should read the manpage first:
"It’s not recommended to use graphical editors (such as gVim, KEdit etc.) due to possible problems with connecting to the X server."

Maybe the author got tired all the requests?

Anyway, thanks for your article.

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 82.131.55.221] on August 26, 2008 01:28 PM
Uh, how are the commands parsed?

If it's anything like the shell, most of the commands should have quotes around the arguments as in rm -rf "$@/$#" to prevent paths with spaces from doing bad things.

#

Scheduling jobs based on filesystem activity with incron

Posted by: Anonymous [ip: 78.86.207.111] on August 26, 2008 06:36 PM
"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."

So surely the first thing you do is write a rule for this? :P

/home/linuxlala/.incrontab IN_CLOSE_WRITE incrontab -d

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya