inotifywait命令


  inotify可以對linux 文件系統進行高效性、細粒度、異步的監控,用於通知用戶控件程序的文件系統變化。inotify可以監控文件,也可以監控目錄,配合rsync實現文件的實時同步功能。

  首先安裝inotify軟件,先檢查自己的系統版本(uname -r),我的是centos 7的系統,我的步驟是

    1、首先檢查自己的電腦是否已經安裝了這個軟件。  rpm -qa inotify-tools

    2、檢查倉庫中是否有這個軟件。  yum search inotify-tools

    3、發現這個軟件不在yum倉庫中,安裝對應的epel源。

        wget -O /etc/yum.repos.d/epel-7.repo  http://mirrors.aliyun.com/repo/epel-7.repo

        yum clean all

        yum makecache

    4、安裝inotify-tools軟件

        yum install inotify-tools -y

    5、查看inotifywait的簡單用法

        

[root@backup ~]# inotifywait --help
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.(daemon模式) -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. (設置打印格式%T時間;%w觸發事件文件所在絕對路徑;%f觸發事件文件名稱;%e觸發的事件名稱;) --timefmt <fmt>    strftime-compatible format string for use with
                      %T in --format string.(指定輸出內容,相當於將時間賦值給%T) -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

舉例:

  監聽/backup/目錄下所有文件和目錄的增刪改操作。打開兩個ssh,一個執行監控操作,另一個對/backup/的文件進行增刪改操作,監控的畫面就會實時輸出修改的結果。

[root@nfs01 data]# inotifywait -mrq -e 'create,delete,close_write,attrib,moved_to' --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' /backup/
2019-06-04 10:46 /backup/test.txt CREATE
2019-06-04 10:46 /backup/test.txt ATTRIB
2019-06-04 10:46 /backup/test.txt CLOSE_WRITE,CLOSE
2019-06-04 10:47 /backup/test.txt CLOSE_WRITE,CLOSE
2019-06-04 10:47 /backup/isr DELETE
2019-06-04 10:47 /backup/me MOVED_TO

 

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM