手机版
你好,游客 登录 注册
背景:
阅读新闻

inotify 实时的Linux文件系统事件监控

[日期:2014-03-16] 来源:Linux社区  作者:Linux [字体:大 中 小]

使用inotify-tools 工具实时监控系统事件监控
inotify-tools
http://linux.die.net/man/1/inotifywait
inotify-tools 下载地址
# wget http://downloads.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
 
监控脚本:
# 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 & 后台执行

推荐阅读:

利用inotifywait监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用inotify+rsync实现Linux文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

linux
相关资讯       inotify 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款