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
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4