歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux下使用inotify監控目錄變動

使用inotify來監控文件變化。inotify是基於事件的監控,無需定時輪詢文件狀態,也無需做內核模塊hook系統調用,簡單可靠。比較好的實現是檢測到文件變化時,同時記錄下當前登陸的用戶以及當前用戶啟動的進程。不過inotify,需要kernel 2.6.13以上支持,RHEL5默認是有的,其它系統不確定。
 
比如:$ dnotify -rCDRM dragon_extfs/ -e echo hello&
 
監控dragon_extfs包括子目錄下所有改動,當發生(C創建,D刪除,R更名,M修改)等改變時執行命令echo hello

事件:
  -A, --access            目標目錄文件被訪問
  -M, --modify            目標目錄文件被改變
  -C, --create            目標目錄文件被創建
  -D, --delete            目標目錄文件被刪除
  -R, --rename            目標目錄文件被更名
  -B, --attrib            目標目錄文件被屬性修改
                       
  -a, --all                以上所有事件
 

參數:
  -e, --execute=COMMAND..  執行命令 一般情況下-e為最後一個參數,-e之後為命令argv
  -f, --file=FILE          監控一個文件目錄列表
  -p, --processes=COUNT    max number of commands to run at a time
  -q, --queue=DEPTH        max depth of queue holding commands to be run
  -t, --times=COUNT        exit after running the command COUNT times
  -o, --once              same as `--times 1'
  -r, --recursive          遞歸子目錄
  -b, --background        後台運行
  -s, --silent            不打印信息
      --quiet              同-s
      --help              display this help and exit
      --version            output version information and exit

Copyright © Linux教程網 All Rights Reserved