To setup inotify is fairly simple:
- apt-get install inotify-tools
- after install, it comes with two module:
- inotifywait - it runs and keep monitor for changes happen in the target folder/file, and the results can be output to an external file.
- inotifywatch - it outputs a summary count of the events received on each file or directory.
- We will use inotifywait to monitor our folder and output a text file in the same folder.
inotifywait /targetFolder -mr -e create,delete,move --timefmt "%D_%a_%H:%M" --format "%T | %w%f | %e" -o /targetFolder/FolderReport.txt --exclude "FolderReport.txt"
- "-m" is used to continuously monitor the target. Without this, by default it will exit after the first event occurs.
- "-e" is to specify the event that we want to monitor. Full list can be viewed by execute inotifywait -h
- "--timefmt" is used to add date and time to the log. By default the result doesn't has such info.
- "--format" has to be specified if --timefmt is used. %T is the time, %w is the path of the folder being monitored, %f is the file name and %e is the event (action done to the folder/file). We can add any words in between the parameter, and it will be output as well. For example, %w the folder is %f %e will output = "/home/tecsun/folder the folder is TestFolder CREATE,ISDIR. (blue is the output specified by parameter)
- "-o" is to output the result to file (else it would display on the terminal)
- "--exclude" is to exclude the result file being monitored, especially when modify event is triggered, so it won't get into infinity loops.
There is one more tools namely iwatch, which is based on inotify, would allow sending of email when event being triggered. Another one which I have not tested is gamin, if anyone of you happen to test this, please let me know how it works.
External links:
- http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html
- A great article with more advanced setup and few other options: http://andries.filmer.nl/kb/Monitoring-file-system-events-with-inotify,-incron-and-authctl/129
- Advanced setup and installation under multiple distros: https://github.com/rvoicilas/inotify-tools/wiki
- http://linuxaria.com/article/introduction-inotify?lang=en
there is a problem it gives me "notifywait: invalid option -- 'o'" why ?
ReplyDeleteI've searched about this issue and haven't found nothing relative to this but in documentation "-o" it is specified
http://linux.die.net/man/1/inotifywait
Hi Vasile, did you specify the output file path?
Deleteinotifywait /var/www/html -mr -e create,delete,move --timefmt "%D_%a_%H:%M" --format "%T | %w%f | %e" -o ~/tmp/html_log_report.txt
ReplyDeleteand ~/tmp/html_log_report.txt it is created and with write access for that user.
and it gives me " inotifywait: invalid option -- 'o' "