inotify工具包有2个重要命令
inotifywatch:使用Linux的inotify接口监听文件系统事件,并输出统计文件或目录接收到的事件
inotifywait:使用Linux的inotify接口高效的监控文件状态是否改变,非常适合搭配shell脚本监控文件
inotifywait命令
格式:inotifywait [-hcmrq] [-e <event> ] [-t <seconds> ] [--format <fmt> ] [--timefmt <fmt> ] <file>
[ ... ]
常用的选项:
选项 | 作用 |
-m, --monitor | 始终保持事件的监听状态 |
-r --recursive | 递归查询目录 |
-q, --quiet | 只打印监控的事件信息 |
--excludei <pattern> | 排除文件或者目录,不区分大小写 |
-t <seconds>, --timeout <seconds> | 超时时间 |
-e <event>, --event <event> | 监听指定事件,有几个常见的事件如下 access 读取文件或目录内容 modify 修改文件或者目录内容 attrib 文件或者目录属性改变 close_write 以可写方式打开的文件被关闭 open 文件或目录被打开 moved_to 文件或目录移动到 moved_from 文件从目录移动 move 移动文件或目录移动到监视目录 create 在监视目录下创建文件或目录 delete 删除监视目录下的文件或目录、 unmount 卸载文件系统 |
--timefmt <fmt> | 自定义时间输出格式 常用的时间格式: %d 日 %m 月 %y 年 %H:%M 时:分 |
--format <fmt> | 自定义inotifywait的输出格式, %w 事件发生时,监控文件或目录的名称信息 %f 事件发生时,将显示监控目录下触发事件的文件 或目录信息,否则为空 %e 显示发生的事件信息,将不同事件信息用逗号隔 开 %T 输出时间格式中定义的时间格式信息,通过 --timefmt 选项的语法格式进行设置 %Xe 显示事件发生的信息,不用的事件用X进行分 ,可以修改X为指定的分隔符 |
示例1:监控/backup目录下创建文件或目录的事件
[root@localhost inotify-tools-3.13]# inotifywait -mrq --timefmt '%d-%m-%y %H:%M' --format '%T %w %f' -e create /backup
30-05-18 07:17 /backup/ data #当在/backup目录下创建目录data时,标准输出了这一条信息 表示inotify捕捉到这个事件了
示例2:监控/backup目录下的多种变化
[root@localhost inotify-tools-3.13]# inotifywait -m /backup
Setting up watches.
Watches established.
/backup/ OPEN,ISDIR
/backup/ CLOSE_NOWRITE,CLOSE,ISDIR
/backup/ CREATE hello_world
/backup/ OPEN hello_world
/backup/ MODIFY hello_world
/backup/ CLOSE_WRITE,CLOSE hello_world
inotifywatch命令:
格式:inotifywatch [-hvzrqf] [-e <event> ] [-t <seconds> ] [-a <event> ] [-d <event> ] <file> [ ...
]
常见的选项
选项 | 作用 |
-v, --verbose | 执行程序期间输出一些额外的错误信息 |
-r, --recursive | 递归选项 |
-t <seconds>, --timeout <seconds> | 设置监控时间 |
-e <event>, --event <event> | 监听指定的事件,常见事件 access 读取文件或目录内容 modify 修改文件或者目录内容 attrib 文件或者目录属性改变 close_write 以可写方式打开的文件被关闭 open 文件或目录被打开 moved_to 文件或目录移动到 moved_from 文件从目录移动 move 移动文件或目录移动到监视目录 create 在监视目录下创建文件或目录 delete 删除监视目录下的文件或目录、 unmount 卸载文件系统 |
-d <event>, --descending <event> | 按指定事件的事件计数降序输出排序,默认总数的降序排序 |
-a <event>, --ascending <event> | 按指定事件的事件计数升序输出排序,默认总数的升序排序 |
示例:统计30s内 /backup目录下的发生事件
[root@localhost inotify-tools-3.13]# inotifywatch -v -t 30 -r /backup
Establishing watches...
Setting up watch(es) on /backup
OK, /backup is now being watched.
Total of 2 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 30 seconds.
total modify attrib close_write open create filename
36 6 4 9 9 8 /backup/
下载的网址:https://sourceforge.net/projects/inotify-tools/
版本为inotify-tools-3.13
mkdir /usr/local/src/inotify
cd /usr/local/src/inotify
然后把压缩包上传到/usr/local/src/inotify目录下
[root@localhost inotify]# ls -l /usr/local/src/inotify/
total 384
-rw-r--r--. 1 root root 389473 May 30 04:12 inotify-tools-3.13.tar.gz
解压安装
tar -zxf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
yum install gcc gcc-devel -y
./configure --prefix=/usr/local/inotify
make && make install
然后添加环境变量
PATH="/usr/local/inotify/bin/:$PATH"
查看一下inotify目录结构
[root@localhost inotify-tools-3.13]# tree /usr/local/inotify -L 1
/usr/local/inotify
├── bin ##相关命令
├── include
├── lib ##函数库
└── share ##帮助文件
可以把帮助文件添加进系统,让用户可以man到
[root@localhost inotify-tools-3.13]#cp /usr/local/inotify/share/man/man1/inotifywait.1 /usr/share/man/man1/inotifywait.1
[root@localhost inotify-tools-3.13]# cp /usr/local/inotify/share/man/man1/inotifywatch.1 /usr/share/man/man1/inotifywatch.1