inotifywait命令


【命令格式】: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
【命令原意】: inote file system wait
【命令路徑】:
【命令功能】: 等待所監聽的文件系統觸發操作事件
【執行權限】: root
【命令描述】:
    眾所周知,Linux 桌面系統與 MAC 或 Windows 相比有許多不如人意的地方,為了改善這種狀況,開源社區提出用戶態需要內核提供一些機制,以便用戶態能夠及時地得知內核或底層硬件設備發生了什么,從而能夠更好地管理設備,給用戶提供更好的服務,如 hotplug、udev 和 inotify 就是這種需求催生的。Hotplug 是一種內核向用戶態應用通報關於熱插拔設備一些事件發生的機制,桌面系統能夠利用它對設備進行有效的管理,udev 動態地維護 /dev 下的設備文件,inotify 是一種文件系統的變化通知機制,如文件增加、刪除等事件可以立刻讓用戶態得知,該機制是著名的桌面搜索引擎項目 beagle 引入的,並在 Gamin 等項目中被應用。

    Inotify 是一個內核用於通知用戶空間程序文件系統變化的機制,是基於inode級別的文件系統監控技術,是一種強大的、細粒度的、異步的機制,它滿足各種各樣的文件監控需要,不僅限於安全和性能,內核要求2.6.13以上,inotify能監控非常多的文件系統事件,通過監控這些事件來監控文件是否發生變更,然后通過rsync來更新發生變更的文件,Inotify 可以監視的文件系統事件包括:

【常用選項】:
    -h 或 --help 顯示幫助

    @<file> Exclude the specified file from being watched.

    --exclude <pattern> 指定排除(忽略)部分文件或目錄,這些文件或目錄上不監聽任何事件,正則表達式書寫(相對路徑)
      FMT: # --exclude 'Runtime' //忽略對Runtime目錄的事件監聽

    --excludei <pattern> 與--exclude相同,區別是此選項正則表達式忽略大小寫

    -m 或 --monitor 持續保持監聽(如果不加此選項,則監聽到一次后便退出)

    -d 或 --daemon 以守護進程方式后台運行(除了在后台運行外,與-m選項一樣)

    -r 或 --recursive 遞歸監聽其下所有子目錄及文件

    --fromfile <file> Read files to watch from <file> or `-' for stdin.

    -o 或 --outfile <file> 將事件輸出到指定文件,而不輸出到屏幕
      FMT: -o /var/log/inotifywait.log

    -s 或 --syslog 將錯誤發送到系統日志,而不是輸出到屏幕

    -q 或 --quiet 打印較少信息(僅打印事件)

    -qq 不打印任何信息(靜默方式)

    --format <fmt> 設置打印屏幕的格式,常見選項:%T時間;%w觸發事件文件所在絕對路徑;%f觸發事件文件名稱;%e觸發的事件名稱;
      FMT: # --format '%T %f %e'

    --timefmt <fmt> 指定輸出時間內容,相當於將時間賦值給%T
      FMT: # --timefmt '%y-%m-%d %H:%M'

    -c 或 --csv 用CSV格式打印事件

    -t 或 --timeout <seconds> 指定一次性監聽時間,超時退出監聽(值為0表示永不超時,單位:秒),不可與 -m -d 連用
      FMT: # -t 60 //設定監聽60秒,60秒內監聽到事件立即退出,如果監聽不到事件60秒后也退出

    -e 或 --event <event1> [ -e|--event <event2> ... ] 指定要監聽的事件(多個事件用逗號分割)
      FMT: # -e 'create,delete,close_write,attrib,moved_to'

【總結提示】:

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.
      意外收到一個未監聽的事件(如:文件自刪除,umount及其它情況)

  2 - The --timeout option was given and no events occurred in the specified interval of time.
      當給定了超時選項,設定時間內沒有事件產生

Events: 事件
  access        file or directory contents were read 文件或目錄被(訪問)讀取r
  modify        file or directory contents were written 文件或目錄被寫入w
  attrib        file or directory attributes changed 文件或目錄屬性變更【理由:chmod更改屬性】
  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 文件或目錄被移動進來【理由:mv目錄內重命名】
  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 文件或目錄被創建【理由:mkdir創建目錄】
  delete        file or directory deleted within watched directory 文件或目錄被刪除【理由:rm刪除】
  delete_self   file or directory was deleted 文件或目錄自刪除
  unmount       file system containing file or directory unmounted 文件系統取消掛載

【參考文檔】:

【選項舉例】:

★ 監聽/wwwroot/web.d目錄內所有文件和目錄的"增刪改"操作

[root@zzx ~]# /usr/local/inotify/bin/inotifywait -mrq -e 'create,delete,close_write,attrib,moved_to' --timefmt '%Y-%m-%d %H:%M' --format '%T %f %e' /wwwroot/web.d/
2014-11-27 17:34 text.txt CREATE
2014-11-27 17:34 text.txt CLOSE_WRITE,CLOSE
2014-11-27 17:34 ttt.t MOVED_TO
2014-11-27 17:35 bb DELETE

 


免責聲明!

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



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