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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With