CentOS 6.5 Inotify-tools Use method

Source: Internet
Author: User
Tags inotify mkdir stdin syslog rsync


The Linux kernel begins with the 2.6.13 and introduces the inotify mechanism. Through the intofity mechanism, can monitor the change of file system, such as the creation, deletion and modification of files, and can notify the application to handle the related events in time. This kind of response processing mechanism avoids the frequent file polling task and improves the processing efficiency of the task.



First, check the system kernel version





[Root@iz25w1kdi5zz ~]# uname-a
Linux iz25w1kdi5zz 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu June to 17:20:51 UTC 2014 x86_ x86_64 x86_64 Gnu/linux

Second, check whether the system supports inotify







[Root@iz25w1kdi5zz ~]# ls-lsart/proc/sys/fs/inotify Total
0
0 dr-xr-xr-x 0 root root 0 Sep 09:38.
0-rw-r--r--1 Root 0,  1 13:51 max_user_watches
0-rw-r--r--1 root root 0 out of  1 13:51 Max_user_instan Ces
0-rw-r--r--1 root 0 is  1 13:51 max_queued_events
0 dr-xr-xr-x 0 root root 0 is out of  1 13:51.


If the above results show that the system supports inotify.



Third, download the installation





[Root@iz25w1kdi5zz src] #wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[ Root@iz25w1kdi5zz src]# tar-zvxf inotify-tools-3.14.tar.gz
[root@iz25w1kdi5zz src]# cd inotify-tools-3.14

[ Root@iz25w1kdi5zz inotify-tools-3.14]#./configure--prefix=/usr/local/inotify
[root@iz25w1kdi5zz inotify-tools-3.14]# make
[root@iz25w1kdi5zz inotify-tools-3.14]# make install
Iv. View inotify Default parameters







[Root@iz25w1kdi5zz bin]# sysctl-a | grep max_queued_events
fs.inotify.max_queued_events = 16384

[root@iz25w1kdi5zz bin]# sysctl-a | grep max_user_ Watches
fs.inotify.max_user_watches = 8192
fs.epoll.max_user_watches = 798863

[Root@iz25w1kdi5zz bin]# sysctl-a | grep max_user_instances
fs.inotify.max_user_instances = 128
V. Modify INOTIFY Parameters





1. Order Modification





[Root@iz25w1kdi5zz bin]# sysctl-w fs.inotify.max_user_instances=130
fs.inotify.max_user_instances = 130
2. File modification




[root@iz25w1kdi5zz]# vi/etc/sysctl.conf
#添加如下代码
fs.inotify.max_user_instances=130
3. Parameter description





Max_user_instances: Create INotify instance maximum per user



Max_queued_events:inotify Queue Maximum Length, if the value is too small, an error occurs, causing the monitor file to be inaccurate



Max_user_watches: To know the number of directories contained in the synchronized file, you can use: [root@iz25w1kdi5zzhome]# find/home/rain-type d|wc-l Statistics, you must ensure that the parameter values are greater than the statistical results (/home/ Rain is the synchronized file directory).



Vi. Creating real-time monitoring scripts





[Root@iz25w1kdi5zz ~]# mkdir-p/opt/scripts

[root@iz25w1kdi5zz ~]# cd/opt/scripts

[root@iz25w1kdi5zz scripts ]# vi inotify_start.sh
/usr/local/inotify/bin/inotifywait-mrq-e modify,create,move,delete \
--fromfile '/ Opt/scripts/ffile '
--timefmt '%y-%m-%d%h:%m '--format '%T%f '%e '--outfile '/tmp/rsync.log '

Inotifywait Common parameters:
--timefmt time format
%y year%m month%d day%h hours%m minutes
--format output format
%t time%w path%f filename%e status-
m Always stay on the listening state, and the default trigger event exits. -
R recursive query directory-
Q print out monitoring events
-e defines monitored events, available parameters: Open
file
Access Access file
Modify modify file
Delete Delete file
Create new file
attrb  Property Change
[Root@iz25w1kdi5zz scripts]# VI ffile
/home/rain/
@/home/rain/cache





[Root@iz25w1kdi5zz scripts]# chmod a+x./inotify_start.sh



Start:



[Root@iz25w1kdi5zz scripts]#./inotify_start.sh





[Root@iz25w1kdi5zz ~]# cd/home/rain
[root@iz25w1kdi5zz rain]# mkdir yy
[Root@iz25w1kdi5zz rain]# cat/tmp/rsync.log
16-01-02 16:21 yy Create,isdir


VII. Appendix



1, inotifywait



instructions for using methods and parameters:


[root@iZ25w1kdi5zZ bin]# ./inotifywait -h
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
        -h|--help       Show this help text.
        @<file>         Exclude the specified file from being watched.
        --exclude <pattern>
                        Exclude all events on files matching the
                        extended regular expression <pattern>.
        --excludei <pattern>
                        Like --exclude but case insensitive.
        -m|--monitor    Keep listening for events forever.  Without
                        this option, inotifywait will exit after one
                        event is received.
        -d|--daemon     Same as --monitor, except run in the background
                        logging events to a file specified by --outfile.
                        Implies --syslog.
        -r|--recursive  Watch directories recursively.
        --fromfile <file>
                        Read files to watch from <file> or `-' for stdin.
        -o|--outfile <file>
                        Print events to <file> rather than stdout.
        -s|--syslog     Send errors to syslog rather than stderr.
        -q|--quiet      Print less (only print events).
        -qq             Print nothing (not even events).
        --format <fmt>  Print using a specified printf-like format
                        string; read the man page for more details.
        --timefmt <fmt> strftime-compatible format string for use with
                        %T in --format string.
        -c|--csv        Print events in CSV format.
        -t|--timeout <seconds>
                        When listening for a single event, time out after
                        waiting for an event for <seconds> seconds.
                        If <seconds> is 0, inotifywait will never time out.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are 
                listened for.

Exit status:
        0  -  An event you asked to watch for was received.
        1  -  An event you did not ask to watch for was received
              (usually delete_self or unmount), or some error occurred.
        2  -  The --timeout option was given and no events occurred
              in the specified interval of time.

Events:
        access          file or directory contents were read
        modify          file or directory contents were written
        attrib          file or directory attributes changed
        close_write     file or directory closed, after being opened in
                        writeable mode
        close_nowrite   file or directory closed, after being opened in
                        read-only mode
        close           file or directory closed, regardless of read/write mode
        open            file or directory opened
        moved_to        file or directory moved to watched directory
        moved_from      file or directory moved from watched directory
        move            file or directory moved to or from watched directory
        create          file or directory created within watched directory
        delete          file or directory deleted within watched directory
        delete_self     file or directory was deleted
        unmount         file system containing file or directory unmounted


2, Inotifywatch


Instructions for using methods and parameters:


[root@iZ25w1kdi5zZ bin]# ./inotifywatch -h
inotifywatch 3.14
Gather filesystem usage statistics using inotify.
Usage: inotifywatch [ options ] file1 [ file2 ] [ ... ]
Options:
        -h|--help       Show this help text.
        -v|--verbose    Be verbose.
        @<file>         Exclude the specified file from being watched.
        --fromfile <file>
                Read files to watch from <file> or `-' for stdin.
        --exclude <pattern>
                Exclude all events on files matching the extended regular
                expression <pattern>.
        --excludei <pattern>
                Like --exclude but case insensitive.
        -z|--zero
                In the final table of results, output rows and columns even
                if they consist only of zeros (the default is to not output
                these rows and columns).
        -r|--recursive  Watch directories recursively.
        -t|--timeout <seconds>
                Listen only for specified amount of time in seconds; if
                omitted or 0, inotifywatch will execute until receiving an
                interrupt signal.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are 
                listened for.
        -a|--ascending <event>
                Sort ascending by a particular event, or `total'.
        -d|--descending <event>
                Sort descending by a particular event, or `total'.

Exit status:
        0  -  Exited normally.
        1  -  Some error occurred.

Events:
        access          file or directory contents were read
        modify          file or directory contents were written
        attrib          file or directory attributes changed
        close_write     file or directory closed, after being opened in
                        writeable mode
        close_nowrite   file or directory closed, after being opened in
                        read-only mode
        close           file or directory closed, regardless of read/write mode
        open            file or directory opened
        moved_to        file or directory moved to watched directory
        moved_from      file or directory moved from watched directory
        move            file or directory moved to or from watched directory
        create          file or directory created within watched directory
        delete          file or directory deleted within watched directory
        delete_self     file or directory was deleted
        unmount         file system containing file or directory unmounted


Related Article

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.