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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
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.