inotify 實時的Linux文件系統事件監控

使用 inotify-tools 工具實時監控系統事件監控
inotify-tools
inotify-tools 下載地址
 
監控腳本:
# cat file_file_inotifywait.sh
#!/bin/sh
inotifywait=/usr/local/bin/inotifywait
monitor_dir=/opt/web/
$inotifywait -mr \
        -e create,move,delete,modify \
        --timefmt '%Y-%m-%d %H:%M' \
        --format '%T %e %w%f ' \
        --exclude upload \
        $monitor_dir > /var/log/file_list
 
 -e create,move,delete,modify 監控系統事件:建立,移動,刪除,修改
--exclude upload 排除upload目錄
 
事件監控日誌  /var/log/file_list
2012-11-16 09:08 CREATE /opt/web/test .php 
2012-11-16 09:08 MODIFY /opt/web/sord.html
能夠詳細查看什麼時間文件建立,或者修改了文件。
 
根據實際狀況,將非系統建立 文件自動刪除
刪除腳本以下:
# cat file_del.sh
#!/bin/sh
file_list=/var/log/file_list
del_file_list=/var/log/del_file_list
while [ true  ]
do
        grep CREATE $file_list > $del_file_list
        while read file
        do
                del=`echo "$file" | awk '{print $4}'`
                if [ -f $del ];then
                        rm -f $del
                fi
        done<$del_file_list
sleep 10
done
  # 執行 ./ file_del.sh & 後臺執行
相關文章
相關標籤/搜索