Use inotify-tools on CentOS 7 or RHEL 7 to watch files and directories for events

Today, I discovered that the package inotify-tools is nowhere to be found in standard CentOS7 or RHEL7 repositories. Alternatives like incron seem to be absent as well. The inotify-tools can be used to watch a directory or file for activity and take an action when a file is changed, added, edited or simply read. You can find a workaround (or call it solution) for the absence of the inotify-tools in the repositories here.

 

Sometimes, for various reasons, a directory needs to be watched for new files, file changes, etc. When a change occurs, a certain action should be taken, like copying the files elsewhere or send an email with to notify someone of the changes. There are a lot of such imaginable scenarios to think of.

inotify

The inotify mechanism which exists in the Linux kernel since release 2.6.13 is ideal for purposes as described above. Inotify notifies the kernel on file changes so it’s not needed to explicitly scan the file system for changes on a regular basis. To use inotify, there are various packages available for different programming languages (like Perl or Python).

inotify-tools

The package inotify-tools contains some tools, written in C, to easily access the inotify-mechanism via the command line or bash. More information about the project can be found here.

Compiling inotify-tools for el7

Apparently, inotify-tools isn’t available (yet) for the latest releases of CentOS and RHEL in any of the popular repositories. In order to use the tools, you’ll have to compile them yourself. One big advantage is that inotify-tools doesn’t have any dependencies besides the kernel with inotify-support, so this shouldn’t be such a big problem.

To start, we need to make sure that we have the necessary tools to compile the sources so we’ll install gcc, cpp, make and their dependencies:

When we’re ready to build the sources, we can download them from Github here
I also created a mirror on http://jensd.be/download/inotify-tools-3.14.tar.gz

After the download, unpack the tar-archive and go into the newly created directory which contains the extracted files:

From here, generate the Makefile by running ./configure:

Once we have a Makefile, we can start building from the inotify-tools source code and generate the executables and libs by executing make:

Once we compiled everything and generated the man-pages, we can do the actual “installation” by copying the generated files to their correct location. This requires superuser rights, so we need to use sudo:

At this time, the inotify-tools should be installed on your machine. Since these tools do not have any external dependencies it’s very unlikely for them to get broken during package upgrades.

 

Using inotify-tools to watch a directory and take action

Now that we have a working installation of the inotify-tools, we can start using them.

The easiest way to use the tools is to create a small bash-script that will monitor a directory and takes action when something is added or changed in that directory. As an example, I created this script, watch-test.sh, that logs all actions that were taken on a certain directory.

Contents of the script:

Don’t forget to make the file executable, then execute the script in the background (by putting the & after the command) and redirect it’s output to /dev/null in order to keep our shell clean:

When do some basic actions on the folder which we are watching, we should see all actions taken in the logfile:

To stop the script, simply bring it back to the foreground and stop it with Ctrl + C:

Looking at the log, we can see that the above executed actions: copy, move, modify and open are logged perfectly to our watchlog.txt

The above script is just a small example what you can do with the inotify-tools. Besides logging you could take other actions like moving, changing or executing something that is related to the files that were touched in the watched directory.

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Understanding logrotate utility part 1
Viewed 828 times since Fri, Nov 30, 2018
ubuntu How to Reset Forgotten Root Password in Ubuntu
Viewed 619 times since Tue, Dec 8, 2020
RHEL: Enabling standard ftp/telnet
Viewed 1599 times since Sun, May 27, 2018
Secure Remote Logging to Central Log Server Using RSYSLOG on CentOS 6 / CentOS 7 and stunnel
Viewed 1112 times since Sun, Dec 6, 2020
Setting up encrypted tunnel using stunnel
Viewed 1103 times since Fri, Sep 28, 2018
LVM: Reduce SWAP size by removing a Logical Volume
Viewed 1017 times since Sat, Jun 2, 2018
Using IOzone for Linux disk performance analysis
Viewed 4868 times since Wed, Jul 25, 2018
haproxy linux
Viewed 680 times since Sun, Dec 6, 2020
Top 25 Best Linux Performance Monitoring and Debugging Tools
Viewed 4120 times since Sun, Sep 30, 2018
RHEL: Crash kernel dumps configuration and analysis on RHEL 5
Viewed 5410 times since Sat, Jun 2, 2018