INotify Monitor directory changes under Linux systems __linux

Source: Internet
Author: User
Tags inotify
Recently, there is a project to delete the Nginx service generated cache files, because it is not very understanding of the Nginx cache generation strategy, on the internet did not look closely, after discussion, eventually want to introduce the Liunx inotify function, monitoring a liunx directory of various events ( Create,delete,access, etc.).
To find out more about INotify's friends, please refer to the following two posts:
1. http://www.ibm.com/developerworks/cn/linux/l-inotify.html uses inotify to monitor Linux file system events
2. http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.html inotify--The file system change notification mechanism in the Linux 2.6 kernel

If you read two blog posts, your idea is to use C language immediately write a monitoring file program (I thought so), first do not busy, look at the following article, immediately introduce you to the Inotify-tools this toolkit, the latest version is the 3.3 version, This toolkit contains almost the directory and file monitoring points, that is, without writing C code, there are already predecessors to help me write it, we can directly through the call to bash script to complete this function.

1, first look at the Linux kernel to support inotify, support inotify kernel minimum of 2.6.13, Input command: Uname–a. As the following illustration shows, the kernel is 2.6.27, and inotify should be supported. If not, I recommend that you choose a high-level Linux kernel. Otherwise there should be a lot of trouble.

2, can also be viewed by the following command to see if the system supports inotify:ll/proc/sys/fs/inotify
If the following output indicates that the system kernel already supports inotify:
Total 0
-rw-r--r--1 root 0 Feb 01:15 max_queued_events
-rw-r--r--1 root 0 Feb 01:15 max_user_instances
-rw-r--r--1 root 0 Feb 01:15 max_user_watches

Download and installation of 3.inotify-tools
Download Address: [Url]http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_ Mirror=0
[/URL]
Installation process: slightly.

4. Introduction of internal orders
The system executes the command: Man inotify, man inotifywait, man Inotifywatch can get the appropriate help information, indicating inotify installation was successful.

Mans INotify:
Capturing various state events for a file system
  Java code     inotify events        Bit                 Description   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  (*)        

Mans inotifywait:
Wait and monitor the status of a directory or file changes, can timely through the Liunx script to wait and monitor the file change events, you can exit the script when the event occurs, you can also output some information when the event occurs.

Parameter description:

--fromfile <file> only monitor changes in file status under the directory
-M,--monitor when an event occurs directly after exiting, the-m parameter will not exit the current shell script.
-R,--recursive recursively monitors all files and directories in the current directory. (the default number of files and directories is 8,192; if not, you can modify/proc/sys/fs/inotify/max_user_watches
--exclude <pattern> through regular matching filename, case sensitive.
--excludei <pattern> through regular matching filename, case insensitive.
-T <seconds> the number of seconds at which the event occurred.
-e <event> Monitor the occurrence of those events
--TIMEFMT option specifies the time format for the output
--format <fmt> output Specify time format.
%w the file name or file path when the event occurs
%f Monitor Directory Internal event occurrence file name
%e monitor the specified event occurrence
%T the time when the output event occurred,--timefmt option specifies the format

Inotifywatch:
Use the Linux inotify feature to monitor file status over a period of time and output summary reports.
Sample: Output Beagle directory 60 seconds of Access and modify Event trigger report
Java Code   % inotifywatch -v -e access -e modify -t 60 -r  ~/.beagle          Establishing watches...          setting up watch (es)  on /home/rohan/.beagle           ok, /home/rohan/.beagle is now being  watched.          Total of 302 watches.           Finished establishing watches, now  collecting statistics.          will listen for  events for 60 seconds.          total   access  modify  filename          1436  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.