曲径通幽论坛

标题: inotifywait -- 监视文件的变动 [打印本页]

作者: beyes    时间: 2012-6-7 12:14
标题: inotifywait -- 监视文件的变动
inotifywait 是一个可以实时监视文件变动的工具。它利用了 Linux 内核中的 inotify 机制实现监视功能,安装它不需要其他软件包的依赖,只要求内核版本高于 2.6.13 即可,它的最新版本为 3.1.4

工具的项目主页为:https://github.com/rvoicilas/inotify-tools/wiki/

在 Fedora 下安装:
[Plain Text] 纯文本查看 复制代码
#yum install inotify-tools


在 Debian/Ubuntu 下安装:
[Plain Text] 纯文本查看 复制代码
apt-get install inotify-tools


比如使用下面对 /root 目录下的监视:
# inotifywait -mr ~
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

现在另一个终端的窗口里对 /root 目录下添加一个文件:
# echo "hello linux" > /root/tmp.txt

那么在运行 inotifywait 的窗口里可以看到:
# inotifywait -mr ~
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/root/ CREATE tmp.txt
/root/ OPEN tmp.txt
/root/ MODIFY tmp.txt
/root/ CLOSE_WRITE,CLOSE tmp.txt
inotifywait 命令中的 -m 选项表示 monitor ,即开启监视。-r 选项表示递归监视,比如上面监视整个 /root 目录,如果在其中的子目录下修改文件也是能被观察到的。

还可以用 -e 选项指定要监控的“事件”(events),比如:
[Plain Text] 纯文本查看 复制代码
inotifywait -rme modify,attrib,move,close_write,create,delete,delete_self > /root/tmp.txt

上面命令中分别指定了多个 “事件” ,如果不用 -e 选项指定,那么默认是监视所有的“事件”。

上面是常用选项参数。更多选项可参考 man 手册。

该工具在实际使用中可以配合 rsync 进行实施备份,甚至可以用来排查服务器上的网站被挂马等情况。

推荐文章:《使用 inotify 监控文件系统的活动》




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2