Linux incrond inotify Monitor Directories For Changes And Take Action

https:/‮www/‬.lautturi.com
Linux incrond inotify Monitor Directories For Changes And Take Action

incron is a Linux utility that allows you to monitor directories for changes and take specific actions when certain events occur, such as a file being modified, deleted, or created. incron uses the inotify kernel subsystem to monitor file system events in real-time.

To use incron, you need to install the incron package by running the following command:

sudo apt-get install incron

Once incron is installed, you can use the "incrontab" command to create and edit incron rules. Each rule consists of an event mask, a path, and a command to be executed when the specified event occurs in the specified path.

Here's an example of an incron rule that monitors the "/tmp" directory for new files and executes the "echo" command whenever a new file is created in the directory:

echo IN_CREATE /tmp/ echo "New file created in /tmp/"

To create this rule, run the following command:

echo "IN_CREATE /tmp/ echo New file created in /tmp/" | incrontab -

To view all the current incron rules, run the following command:

incrontab -l

To delete all the current incron rules, run the following command:

incrontab -r

Note: incron rules are stored in a user-specific configuration file, so you need to run the above commands as the user for whom you want to create or delete the rules. Additionally, you need to make sure that the incron daemon is running for the rules to take effect. To start the incron daemon, run the following command:

sudo service incron start
Created Time:2017-10-30 10:17:41  Author:lautturi