Monitor file and directory activity with incron
Dynamic incrontab
Something else you can do is use incron to tell users when they have new documents on a server waiting to be processed. For example, imagine you're the editor in chief of a famous Linux magazine, and you need to tell your editors when articles are available for editing, correcting, proofreading, and so on. Instead of copying the documents to the server, firing up your email client, and manually shooting off a message to the concerned party, you just want to copy the document and be done with it. You could hope that your staff will check their online folders regularly, but you can't afford to assume that will happen. That is why we script.
Instead of dumping all the documents willy-nilly into one directory, you want to be able to monitor each of the users' home directories on the server and then monitor the subdirectories that are created for each magazine and issue within those directories.
This sets another challenge: As mentioned before, incron does not implement recursivity, so you can't just monitor the top level and bore down when a folder for a new issue is created. If you monitor the users' top-level directories, incron won't see when a file is added to an issue subdirectory, so it seems you will have to create an incrontab line for each new subdirectory you create in a user's home folder, and that seems as bothersome as emailing each editor by hand – unless you write a script to do it for you.
The trick is to get your script to write directly into the users' incrontab files without having to use incrontab -e
. The inrontab files live at /var/spool/incron/
, and each user has their own with their name on it. Thus, user joe's incrontab will be /var/spool/incron/joe
, user jane's file will be /var/spool/incron/jane
, and so on. To accomplish this, you use a global, superuser incrontab with one line per editor to write in each file. The rules in root's incrontab will look something like this:
/home/joe IN_CREATE /usr/local/bin/makeIncrontab.sh $@ /home/jane IN_CREATE /usr/local/bin/makeIncrontab.sh $@ /home/jed IN_CREATE /usr/local/bin/makeIncrontab.sh $@ ...
Yes, you do have to create one rule per editor, but unless you're continually hiring and firing editorial staff, you won't have to change this file very often. Each line monitors the users' home directories and calls the makeIncrontab.sh
script (Listing 2) whenever a subdirectory is created in any of the monitored directories.
Listing 2
makeIncrontab.sh
The makeIncrontab.sh
script takes as an argument the name of the user from the user's monitored home directory (culled from the $@
variable included in each of root's incrontab rules) and removes the existing incrontab file for that user from the /var/spool/incron/
directory. Then, it loops over all the subdirectories in the user's home directory and creates a new file with rules for each subdirectory, indicating that the user must be mailed if a new file is added to any of those subdirectories.
The user's email address is extracted from the file .emailaddress
that you will have created previously and placed in the user's home directory. The beauty of removing the user's incrontab file and then creating it anew every time the script is run is that the rules that refer to old directories that have been deleted get flushed out of the incrontab file.
Say, over time, you have created subdirectories in Joe's home directory for Linux Magazine 75 (lm75
), Linux Magazine 76 (lm76
), Shell Special 02 (shell02
), and Ubuntu User 04 (uu04
). His personal incrontab file will end up looking like Listing 3.
Listing 3
User joe's incrontab File
The send_mail.sh
script, by the way, you'll have to write yourself. Mine is actually a Python (not Bash) script, and it sends a message with instructions on how to download the files via SSH, complete with tailored command lines (thanks again to the $@
variable) that the editors can copy and paste into a terminal.
Conclusion
Incron is fun! It gives you a very complete and powerful set of tools for monitoring and reacting to changes in your filesystem, saving you a ton of work and trouble by allowing you to link events and tasks.
Infos
- incron: http://inotify.aiken.cz/?section=incron&page=about&lang=en
- "Command Line: at and cron" by Heike Jurzik, Linux Magazine, April 2006, pg. 82: http://www.linux-magazine.com/Issues/2006/65/Command-Line-at-and-cron/%28language%29/eng-US
- incron download: http://inotify.aiken.cz/?section=incron&page=download&lang=en
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.
-
Plasma Desktop 6.1.4 Release Includes Improvements and Bug Fixes
The latest release from the KDE team improves the KWin window and composite managers and plenty of fixes.
-
Manjaro Team Tests Immutable Version of its Arch-Based Distribution
If you're a fan of immutable operating systems, you'll be thrilled to know that the Manjaro team is working on an immutable spin that is now available for testing.