How to Monitor Filesystem Events with incron
Sometimes there is just a need to monitor specific files or filesystems and perform actions or receive notifications, this is where incron can help.
incron is for monitoring filesystem activity. It consists of a daemon and a table manipulator. 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
incron provides a simple way how to solve many and many various situations. Every time when something depends on file system events, it’s a job for incron.
Here you can see a few examples where incron is a good solution:
- Notifying programs (e.g. server daemons) about changes in configuration
- Guarding changes in critical files (with their eventual recovery)
- File usage monitoring, statistics
First we will need to install incron:
$ sudo yum install incron
Make sure we set it to start on reboot:
$ sudo chkconfig incrond on
And now to start incron:
$ sudo service incrond start
incrond uses inotify. So to use it effectively we need to have it act on inotify events which are:
IN_ACCESS File was accessed (read) (*). IN_ATTRIB Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*). IN_CLOSE_WRITE File opened for writing was closed (*). IN_CLOSE_NOWRITE File not opened for writing was closed (*). IN_CREATE File/directory created in watched directory (*). IN_DELETE File/directory deleted from watched directory (*). IN_DELETE_SELF Watched file/directory was itself deleted. IN_MODIFY File was modified (*). IN_MOVE_SELF Watched file/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 (*).
The incron table manipulator may be run under any regular user since it SUIDs. For manipulation with the tables use basically the same syntax as for the crontab program. You can import a table, remove and edit the current table.
The user table rows have the following syntax (use one or more spaces between elements):
<path> <mask> <command>
<path> is a filesystem path (each whitespace must be prepended by a backslash)
<mask> is a symbolic or numeric mask for events (see man inotify for more details)
<command> is an application or script to run on the events
The command may contain these wildcards:
$$ - a dollar sign $@ - the watched filesystem path (see above) $# - the event-related file name $% - the event flags (textually) $& - the event flags (numerically)
Now with all that information, what can I do? Say you want to be notified each time /etc/hosts is modified and email us. Open incrontab make sure you are root for this example:
# incrontab -e /etc/hosts IN_MODIFY mailx -s "Hosts file Has Been modified" email@example.com
Save the changes and open /etc/hosts and make a change and you should receive an email in your inbox.
At this point we have covered just the basics of what is possible with incron. Experiment with incron and see what other items you can monitor and what other commands you can execute on filesystem actions.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Mount Remote Filesystems Over SSH Using SSHFS : We, all, use NFS, Samba and OpenAFS to mount and access remote file systems over network. Unfortunat...0 comments |
- How To Create An ISO Image With genisoimage Tool : Dear unixmen reader, In this article I will teach you how to create an iso image from your files wi...0 comments |
- Install GParted 0.17.0 in Ubuntu 13.10 : GParted is a free partition manager that enables you to resize, copy, and move partitions without da...0 comments |
- Install Code::Blocks IDE in Ubuntu/ElementaryOS/Debian : Code::Blocks is an open-source, cross-platform IDE. If you are beginning or learning C/C++ Code::Blo...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install GParted 0.17.0 in Ubuntu 13.10
- Install Code::Blocks IDE in Ubuntu/ElementaryOS/Debian
- How To Create An ISO Image With genisoimage Tool
- Mount Remote Filesystems Over SSH Using SSHFS
- Install Radio Tray In Ubuntu
- How To Install Liferea Feed Reader In Ubuntu
- How To Install PhotoFilmStrip 2.0 In Ubuntu
- Install Flowblade Movie Editor In Ubuntu
- How To Install The Latest QMPlay2 In Ubuntu
- Guake: A Drop-Down Terminal For GNOME In Ubuntu
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .