Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continuously monitor the directory using dnotify /inotify command

I am new to dnotify/inotify command. Can any one help me how to write a script such that it continuously monitors a directory and indicates that there is some change or modification to it.

like image 268
sai sindhu Avatar asked Sep 27 '11 09:09

sai sindhu


People also ask

How to monitor a directory in Linux?

In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file. When we add a watch to a file, we can monitor it. For example, we'll know when a process opens, modifies, reads closes, moves, or deletes the file.

What is inotify in Linux?

inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.

How is inotify implemented?

With Inotify, anti-virus detectors re-scan the file system for modified files to detect if any malicious intrusions have occurred. This kind of applications use a user-space device through which Inotify events are triggered between the kernel and user-space applications.

How to monitor file changes in Linux?

Monitoring file changes in a real time is very easy to do task in Linux System. Directory, files, logs, etc. Changes can be easily monitored in real time with the help of watch command. Watch is easy to use program to monitor changes in file or directory in Linux.


1 Answers

Inotify itself is a kernel module accesible via calls from e.g. a C program. http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/

There is an application suite called inotify-tools, which contains:

inotifywait - wait for changes to files using inotify

http://linux.die.net/man/1/inotifywait

and

inotifywatch - gather filesystem access statistics using inotify

http://linux.die.net/man/1/inotifywatch

You can use inotify directly from command line, e.g. like this to continuously monitor for all changes under home directory (may generate lots of output):

inotifywait -r -m $HOME 

And here is a script that monitors continuously and reacts to Apache log activity, copied from the man file of inotifywait:

#!/bin/sh while inotifywait -e modify /var/log/messages; do   if tail -n1 /var/log/messages | grep httpd; then     kdialog --msgbox "Apache needs love!"   fi done 
like image 95
thnee Avatar answered Sep 21 '22 23:09

thnee