Using Incron To Schedule Tasks For File System Changes
When running a server, it’s quite a common requirement to automate tasks. Linux distributions provide the cron tool, allowing you to schedule tasks at specific times. One downside to cron is that you may want tasks performed that require triggers other than just a pre-set time being reached. Fortunately, there are a number of related tools for scheduling tasks, one of which is incron. Incron uses the inotify system to check for file and directory modifications, and can then trigger tasks based on a set of rules defined for it.
Installing Incron
Incron itself is in the default repositories for many Linux distributions, and for Debian and Ubuntu systems installation is as simple as:
“`
sudo apt-get update
sudo apt-get install incron
“`
For CentOS and Red Hat Linux distributions it can be installed with:
“`
sudo yum install incron
“`
As with cron, each user can create their own incron table file that they can configure to have tasks performed for them. You can limit which users can use incron with an allow/deny system. If the file */etc/incron.allow* exists, then only users who are listed in that file will have their incron file processed. Similarly, if the file */etc/incron.deny* exists, all users will have their incron file processed unless the user is listed in the file.
Setting Up Incron
To set up an incron table file the user can use the incrontab command, similar to using the normal crontab command. For example, editing the incron table file can be done with the following command:
“`
incrontab -e
“`
The file consists of lines in the following format that set the rules that incron should follow:
“`
<path> <mask> <command>
“`
The line starts with a path. This should be the full path of a file or directory to be monitored. If you set a directory for monitoring, then only the files in that directory itself are monitored, not files within subdirectories. Therefore, you would need to set additional rules for any subdirectories that also need monitoring. The line ends with the command that should be run when incron detects a change in the monitored file or directory it needs to respond to. Between these, we have the mask which defines what kind of event should occur for incron to run the command.
Here is a list of valid masks you could use:
IN_ACCESS File was accessed (read) (+)
IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.) (+)
IN_CLOSE_WRITE File that was opened for writing was closed (+)
IN_CLOSE_NOWRITE File that was opened for read-only was closed (+)
IN_CREATE A file or directory created in watched directory (+)
IN_DELETE A file or directory deleted from watched directory (+)
IN_DELETE_SELF Watched file or directory was itself deleted
IN_MODIFY File was modified (+)
IN_MOVE_SELF Watched file or directory was itself moved
IN_MOVED_FROM File moved out of watched directory (+)
IN_MOVED_TO File moved into watched directory (+)
IN_OPEN File was opened (+)
IN_ALL_EVENTS Matches all the above
Mask options marked with a plus symbol (+) are valid both for the directory being watched and the contained files and directories. Incron is also capable of passing some information across to the command being called in order for commands to act upon it. The following symbol codes can be placed in the command path as arguments:
$$ dollar sign
$@ watched filesystem path
$# event-related file name
$% event flags (textually)
$& event flags (numerically)
These symbol codes mean that you can write scripts that can respond dynamically to the events that trigger incron.
With incron’s uses now covered, let’s look at some examples of how you might use incron in a server situation:
“`
/etc/php/7.0/apache2/php.ini IN_MODIFY /usr/sbin/service apache2 restart
“`
This example is designed for Debian or Ubuntu systems and would instruct incron to call the Apache web server to restart upon making modifications to the PHP configuration file.
“`
/var/run IN_MODIFY /home/myuser/myscript $# $%
“`
Here we have another example which monitors the `/var/run` directory for changes which might occur should a process restart. It will then call the script in `/home/myuser/myscript`, passing it the arguments of the filename that changed and the flag that triggered it. This could be used to create a general monitoring script to alert you if changes happen to your running processes.
Incron can provide a lot of flexibility to scheduling tasks that could be difficult to achieve using traditional time-based cron or similar tools. As such it’s something to keep in mind when automating and scheduling tasks.