CentOS 6.5上inotify-tools使用方法

來源:互聯網
上載者:User

Linux核心從2.6.13開始,引入了inotify機制。通過intofity機制,能夠對檔案系統的變化進行監控,如對檔案進行建立、刪除、修改等操作,可以及時通知應用程式進行相關事件的處理。這種響應處理機制,避免了頻繁的檔案輪詢任務,提高了任務的處理效率。

一、檢查系統核心版本

[root@iZ25w1kdi5zZ ~]# uname -aLinux iZ25w1kdi5zZ 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

二、檢查系統是否支援inotify

[root@iZ25w1kdi5zZ ~]# ls -lsart /proc/sys/fs/inotifytotal 00 dr-xr-xr-x 0 root root 0 Sep 19 09:38 ..0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_watches0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_instances0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_queued_events0 dr-xr-xr-x 0 root root 0 Jan  1 13:51 .

如果出現上面結果說明系統支援inotify。

三、下載安裝

[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
四、查看inotify預設參數

[root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_queued_eventsfs.inotify.max_queued_events = 16384[root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_user_watchesfs.inotify.max_user_watches = 8192fs.epoll.max_user_watches = 798863[root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_user_instancesfs.inotify.max_user_instances = 128
五、修改inotify參數

1、命令修改

[root@iZ25w1kdi5zZ bin]# sysctl -w fs.inotify.max_user_instances=130fs.inotify.max_user_instances = 130
2、檔案修改

[root@iZ25w1kdi5zZ]# vi /etc/sysctl.conf#添加如下代碼fs.inotify.max_user_instances=130
3、參數說明 

max_user_instances:每個使用者建立inotify執行個體最大值

max_queued_events:inotify隊列最大長度,如果值太小,會出現錯誤,導致監控檔案不準確

max_user_watches:要知道同步的檔案包含的目錄數,可以用:[root@iZ25w1kdi5zZhome]# find /home/rain -type d|wc -l 統計,必須保證參數值大於統計結果(/home/rain為同步檔案目錄)。

六、建立即時監控指令碼

[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常用參數:--timefmt 時間格式%y年 %m月 %d日 %H小時 %M分鐘--format 輸出格式%T時間 %w路徑 %f檔案名稱 %e狀態-m 始終保持監聽狀態,預設觸發事件即退出。-r 遞迴查詢目錄-q 列印出監控事件-e 定義監控的事件,可用參數:open 開啟檔案access 訪問檔案modify 修改檔案delete 刪除檔案create 建立檔案attrb  屬性變更
[root@iZ25w1kdi5zZ scripts]# vi ffile/home/rain/@/home/rain/cache

[root@iZ25w1kdi5zZ scripts]# chmod a+x ./inotify_start.sh

啟動:

[root@iZ25w1kdi5zZ scripts]#  ./inotify_start.sh

[root@iZ25w1kdi5zZ ~]# cd /home/rain[root@iZ25w1kdi5zZ rain]# mkdir yy
[root@iZ25w1kdi5zZ rain]# cat /tmp/rsync.log16-01-02 16:21 yy CREATE,ISDIR

七、附錄

1、inotifywait

使用方法和參數說明:

[root@iZ25w1kdi5zZ bin]# ./inotifywait -hinotifywait 3.14Wait 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

使用方法和參數說明:

[root@iZ25w1kdi5zZ bin]# ./inotifywatch -hinotifywatch 3.14Gather 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











相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.