Monitor file and directory activity with incron

Controlled Files

© Lead Image © Peapop, Fotolia.com

© Lead Image © Peapop, Fotolia.com

Article from Issue 158/2014
Author(s):

The incron utility provides an easy way to initiate commands and scripts triggered by filesystem events.

If you want to perform a task every time a specific event occurs, you could employ various techniques for polling or log watching; however, Linux provides a more general solution in the form of a tool called incron. Incron uses the inotify subsystem [1] to listen for events that affect a filesystem, such as opening, creating, or deleting a file; accessing a directory; or changing an attribute.

The name incron suggests the common cron utility [2]; however, cron jobs are triggered by a moment in time (every Friday, once a day at 3 am, in August, etc.), whereas incron is triggered by file or directory events. The gears in your head are probably already spinning, thinking of all the potential uses of incron. So, in this article, I describe how to set up incron and put it to work in some simple examples.

Getting incron

The incron tool set is not usually preinstalled by default in most distros, but because it is an implementation of the inotify subsystem (see the box "The Origin of incron"), it is likely in your distro's repository, so you can install it through the package manager. However, if for some reason you can't install the necessary packages from your distro's repos, you can download the latest version [3] and build from source.

The Origin of incron

The in in incron comes from inode, the data structure that contains the details (location on the physical disk, size, owner, etc.) of each file and directory in the filesystem. Inotify is the kernel subsystem on which incron is based. This subsystem monitors file and directory changes and can be queried by applications, allowing, for example, a new folder icon to pop up in real time in a graphical file browser window (e.g., Nautilus or Dolphin), even if the underlying directory has been created by some other means (e.g., with mkdir from a shell). It can also be used to inform an application that an open file has been changed on disk by some other program, thus allowing the user to overwrite or rename the version being worked on.

Note that incron is a relatively recent technology – the inotify notification framework was first implemented in kernel 2.6.13 – so if you're running an older kernel, incron will not work.

Incron comprises several bits and pieces, of which the main component is the incrond daemon. This daemon installs into /etc/init.d. Depending on your system, it can be started with one of the following commands

# /etc/init.d/incrond start
# /etc/init.d/incron start

or, if you're using systemd,

# systemctl status incron.service

or any variation thereof.

Hello incron!

Once the daemon is running, you can create your own particular "Hello World!" In this first project, I'll create a logfile and add a line to it every time a file is added to a monitored directory.

First, I create the directory I want to monitor:

$ mkdir my_dir

The incron system is similar to cron in that it saves a file for each user. This file, called incrontab, contains the events you want to monitor linked to the actions you want to run. Although you could edit this file by hand (I'll look at that a bit later), the done way of doing things is to use the incrontab instruction with, in this case, the -e (for edit) argument:

incrontab -e

This command opens the file for the current user in the preconfigured default editor – that is, whatever your $EDITOR environment variable points to (or vi if it isn't pointing to anything). As with cron, each event/task pair has to be on one line and comprises:

  1. <a path> – the path to the directory or file to monitor.
  2. <a mask> – the events to monitor (see Table 1).
  3. <a command> – the action to run. It can be an individual command or a script that runs from the shell.

Table 1

Monitored Events

Event

Meaning

Common Events

IN_ACCESS

File was accessed (read)

IN_ATTRIB

Metadata was changed (permissions, timestamps, extended attributes, etc.)

IN_CLOSE_WRITE

File opened for writing was closed

IN_CLOSE_NOWRITE

File not opened for writing was closed

IN_CLOSE

Combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE

IN_CREATE

File/directory created in watched directory

IN_DELETE

File/directory deleted from watched directory

IN_DELETE_SELF

Watched file/directory was deleted

IN_MODIFY

File was modified

IN_MOVE_SELF

Watched file/directory was moved

IN_MOVED_FROM

File moved out of watched directory

IN_MOVED_TO

File moved into watched directory

IN_MOVE

A combination of IN_MOVED_FROM and IN_MOVED_TO.

IN_OPEN

File was opened

Special Events

IN_ALL_EVENTS

Combines all of the above events

IN_DONT_FOLLOW

Don't dereference pathname if it is a symbolic link

IN_ONESHOT

Monitor pathname for only one event

IN_ONLYDIR

Only watch pathname if it is a directory

Wildcard Event

IN_NO_LOOP

Disable monitoring of events until the current event is handled completely (until its child process exits – avoids infinite loops)

In this example, incrontab will contain a single line:

/home/<user>/my_dir IN_CREATE /home/<user>/bin/hello.sh

The hello.sh file contains the very short script,

#!/bin/bash
echo "File created." >> /home/<user>/file_log

where <user> is the username of interest. For this command to work, you must make hello.sh executable with:

chmod a+x bin/

Once you have saved and closed the user's incrontab file, the program will show the message table updated, indicating the changes have been registered. Now, if you create a file in my_dir by typing, for example,

$ touch my_dir/file1.txt

the file_log file will appear in your home directory containing the line File created.

Note the first limitation of incron: Compound operations, such as the echo instruction that redirects its output to a file in hello.sh, must be wrapped in shell script, or incron will choke. Because incron is not a shell command interpreter, it does not understand such things as the redirections, pipes, and globbings that make up a compound instruction. The only thing incron can use in the command section of the line is the instruction's name and its parameters. For example, this command

/home/paul/my_dir IN_CREATE echo "File created." >> /home/paul/file_log

would not work as expected, because it would be interpreted as follows:

  • echo is the instruction to run (correct).
  • "File created." is a parameter for echo (also correct).
  • >> is another argument for echo (incorrect).
  • /home/paul/file_log is also an argument for echo (also incorrect).

This behavior is the default for incron, so it won't generate any errors in its logs. (You can track incron's errors or lack thereof by checking /var/log/cron.) Incron simply ignores what it doesn't understand and carries on.

This example also illustrates the second limitation of incron: In the incrontab line, you have to specify the complete absolute path to the executable that, in this case, lives in the user's own bin/ directory. Although this directory might be included in the user's own $PATH environment variable, the incron daemon runs as superuser, so the executable would have to be in the root's $PATH for the daemon to find it; otherwise, you have to specify the absolute path. Thus, you must make sure your executable is in /bin, /usr/bin, /usr/local/bin, or somewhere equally accessible, or point to the program with an absolute path.

Wild Cards

This "Hello World" incron program is as silly as any other, but with a couple of small changes you can modify it to make it much more useful. For example, registering the name of a created file in the logfile is easy. Incron provides a series of default variables (for some reason called "wildcards" in incron jargon) that contain this kind of information. (See Table 2 for a full list.) For the time being, $#, which contains the name of the file affected by the event, is of interest.

Table 2

Wildcards

Wildcard

Meaning

$$

Dollar sign

$@

Watched filesystem path

$#

Event-related file name

$%

Event flag (textual)

$&

Event flag (numerical)

Open your incrontab file again (incrontab -e) and change your monitoring line to:

/home/paul/my_dir IN_CREATE /home/paul/bin/hello.sh $#

Next, modify your hello.sh script to:

#!/bin/bash
echo "File $1 created." >> /home/paul/file_log

Now try it out by creating a new file in my_dir:

$ touch my_dir/file2.txt

If you look in file_log, you'll see that it contains a new line that says File file2.txt created..

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Charly's Column

    While cron doggedly keeps to a fixed schedule, Incron monitors directories and runs commands when changes occur.

  • Charly's Column: iWatch

    Recently, sys admin Charly was faced with the task of synchronizing a directory on a server with two NFS-mounted clients. He wanted the whole thing to happen quickly and to be easily manageable, which ruled out DRBD and GlusterFS.

  • Security Lessons: System Rescue

    Kurt provides some tips and recommends some tools to help you detect signs of network intrusion and data corruption.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News