Incron command in Linux with Examples
Last Updated :
21 Jun, 2022
Incron is an “inotify cron” system. It works very much like the regular cron, however, cron jobs are triggered by a moment in time (every Sunday, once a day at 12 am and so on), whereas incron jobs are triggered by filesystem events (such as creating, deleting or modifying files or directories). Here are few examples where incron can be used:
- Monitoring file usage and statistics
- Notifying programs (e.g. server daemons) if changes are made in the configuration files.
- Protecting against changes in the critical files.
- Automatic on-change backup or versioning.
- Processing uploaded files.
Note: As incron is not recursive, so you have to also add all the sub-directories that you want it to watch. Do not do any action from within an incron job in a directory that you monitor to avoid loops.
Installing and Configuring Incron:
Use the following command to install incron.
$sudo apt-get install incron
Configuration: To configure incron access we have to configure /etc/incron.allow and /etc/incron.deny files.
- /etc/incron.allow : If this file exists only users listed here may use incron.
- /etc/incron.deny : If this file exists only users NOT listed here may use incron.
If none of these files exists every user on the system is allowed to use incron.
Syntax:
<path> <mask> <command>
Here:
- <path>is absolute path of the directory to watch.
- <mask> is event mask(in symbolic or numerical form).
Event Symbols (Masks):
IN_ACCESS File was accessed (read).
IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.).
IN_CLOSE_WRITE File opened for writing was closed.
IN_CLOSE_NOWRITE File not opened for writing was closed.
IN_CREATE File/directory created in watched directory.
IN_DELETE File/directory deleted from watched directory.
IN_DELETE_SELF Watched file/directory was itself deleted.
IN_MODIFY File was modified.
IN_MOVE_SELF Watched file/directory was itself moved.
IN_MOVED_FROM File moved out of watched directory.
IN_MOVED_TO File moved into watched directory.
IN_OPEN File was opened.
- <command> is executable file (or script) with its arguments.
The following wildcards may be used inside the command specification.
$$ Prints a dollar sign
$@ Add the watched filesystem path
$# Add the event-related file name
$% Add the event flags (textually)
$& Add the event flags (numerically)
Setting Up Incron
After installation and configuration, you need to start incron daemon by using the following command:
$ /etc/init.d/incrond start
OR
$ systemctl start incron.service
You can check incron status using:
$ systemctl status incron.service

Now you have to add your user or root to the /etc/incron.allow config. file so that user can access crontab.
$ sudo nano /etc/incron.allow
Using incrontab commands you can list (-l), edit (-e), and remove (-r) incrontab entries.
$ incrontab -l
$ incrontab -e
$ incrontab -r
Monitoring a file
Suppose, we want to monitor a file( /tmp/gfg/myfile.txt ) and for every change, we want to log the date and time of the modification to a log file( /tmp/logs/mylogs.txt ).
1. The content of “myfile.txt” are displayed below.

2. Create a script with name “action.sh” that will log the date and time to the log file, we will run this script every time changes are made to the myfile.txt.

3. Now you can edit crontab using command.
$ crontab -e:


4. Now let’s make changes to myfile.txt.

5. If we see logs we can see the date and time of changes.

Monitoring a directory
Now let’s monitor directory( /tmp/gfg/) and for every new File/directory we log the date and time of the creation to a log file( /tmp/logs/mylogs.txt ).
1. Open incrontab file. Here we are passing “$#” wildcard in the command which will pass the event-related file name to the command.


2. We will update action.sh. Here we are using “$#” wildcard which was passed as a argument.

3. Now let’s try it out by creating a new file in our gfg directory and see the resultant in log file:

Similar Reads
info command in Linux with Examples
info command reads documentation in the info format. It will give detailed information for a command when compared with the man page. The pages are made using the Texinfo tools which can link with other pages, create menus, and easy navigation. Here, we will explore the functionality of the info com
3 min read
ipcrm command in Linux with examples
ipcrm command in Linux is used to remove some IPC(Inter-Process Communication) resources. It eliminates the IPC objects and their associated data structure form the system. One must be a creator or superuser or the owner of the object in order to remove these objects. There are three types of System
2 min read
insmod command in Linux with examples
insmod command in Linux systems is used to insert modules into the kernel. Linux is an Operating System that allows the user to load kernel modules on run time to extend the kernel functionalities. LKMs(Loadable Kernel Modules) are usually used to add support for new hardware (as device drivers) and
3 min read
if command in linux with examples
if command in Linux is used for conditional execution in shell scripts.The if command is essential for writing scripts that perform different actions based on different conditions. if COMMANDS list is executed, if its status is true, then the then COMMANDS list is executed. Otherwise, each elif COMM
4 min read
fc Command in Linux with Examples
As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the u
2 min read
gcc command in Linux with examples
GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++. The most important option required while compiling a source code file is the name of the source program, rest every argument is optional like a wa
2 min read
let command in Linux with Examples
The `let` command in Linux is a powerful tool used for evaluating arithmetic expressions on shell variables. It supports various operators and functionalities to perform calculations and manipulate values. Syntax of `let` command in Linux The basic syntax of the `let` is as follows. let [expression]
3 min read
for command in Linux with Examples
IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of
2 min read
expr command in Linux with examples
The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions. What is the 'expr' Command?expr stands for "expression" an
3 min read
dc command in Linux with examples
The dc command is a versatile calculator found in Linux systems, operating using reverse Polish notation (RPN). This command allows users to perform arithmetic calculations and manipulate a stack, making it ideal for complex mathematical tasks directly from the command line. SyntaxThe basic syntax f
3 min read
ex command in Linux with examples
ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transf
4 min read
ed command in Linux with examples
Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
4 min read
as command in linux with examples
as command is the portable GNU assembler in Linux. Using as command, we can read and assemble a source file. The main purpose of 'as' is to assemble the output of the GNU compiler of the C language.as command reads and assembles the .s File. Also, whenever you do not specify the file, it normally re
3 min read
exec command in Linux with examples
The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts. What is the exec Comman
3 min read
help Command in Linux with examples
If you’re new to the Linux operating system and struggling with command-line utilities, the help command is one of the first tools you should learn. As its name suggests, the 'help' command provides detailed information about built-in shell commands, making it an essential resource for beginners and
5 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhost
1 min read
less command in Linux with Examples
The 'less' command in Linux is an indispensable utility for browsing the contents of text files interactively. Unlike traditional text editors, 'less' allows you to view text files page by page without loading the entire file into memory. This approach not only speeds up the file loading process, es
5 min read
nl command in Linux with Examples
Linux offers a wide range of commands for text formatting and editing. While editing a text file, you might want to display the lines with line numbers appended before them, and here comes the role-play of the nl command in Linux. nl command is a Unix/Linux utility that is used for numbering lines,
4 min read