inotify是linux文件系统事件监控机制,功能强大,控制简单,可以实现很多有用的功能。如:当一个文件被访问、打开、关闭、移动、删除等等时做一些处理。此功能需要内核支持,从kernel 2.6.13开始正式并入内核,RHEL5已经支持。

查看系统是否支持此功能:
[root@demo ~]# ls -l /proc/sys/fs/inotify
总计 0
-rw-r--r-- 1 root root 0 12-08 05:42 max_queued_events
-rw-r--r-- 1 root root 0 12-08 05:42 max_user_instances
-rw-r--r-- 1 root root 0 12-08 05:42 max_user_watches

如果有inotify目录并下面有这三个文件,说明内核支持此功能

要想使用此功能还需要一个工具(inotify-tools),来控制内核的这种功能,就是内核的具体实现。
工具下载地址:http://sourceforge.net/directory/os:windows/freshness:recently-updated/?q=inotify-tools
工具的安装:
[root@demo ~]# tar zxvf inotify-tools-3.13.tar.gz
[root@demo ~]# cd inotify-tools-3.13
[root@demo inotify-tools-3.13]# ./configure
[root@demo inotify-tools-3.13]# make
[root@demo inotify-tools-3.13]# make install

安装完毕,默认安装到/usr/local,可以通过./configure --prefix=PATH更改

工具集介绍:
一共安装了2个工具(命令),即inotifywait和inotifywatch
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。
inotifywatch:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。
inotifywait使用例子:
监视web根目录,用浏览器访问时出现下列事件
[root@demo ~]# inotifywait -mrq /usr/local/nginx/html/
/usr/local/nginx/html/ OPEN index.html
/usr/local/nginx/html/ CLOSE_NOWRITE,CLOSE index.html
/usr/local/nginx/html/ OPEN info.php
/usr/local/nginx/html/ CLOSE_NOWRITE,CLOSE info.php
/usr/local/nginx/html/ OPEN info.php
/usr/local/nginx/html/ CLOSE_NOWRITE,CLOSE info.php
/usr/local/nginx/html/ OPEN info.php
/usr/local/nginx/html/ CLOSE_NOWRITE,CLOSE info.php

参数:

-m | --monitor:一直监视指定目录,如果没有这个选项,inotifywait在接收到一个事件后就退出了。
-r | --recursive:递归监视指定目录,即包括所有子目录。
-q | --quiet:输出少量信息(只输出事件信息,无附加开头的说明信息)
inotofywatch使用例子:
监视统计web根目录,用浏览器访问时出现下列事件
[root@demo ~]# inotifywatch -rz -a open -t 6 /usr/local/nginx/html/
Establishing watches...
Finished establishing watches, now collecting statistics.
total    access    modify    attrib    close_write    close_nowrite    open    moved_from    moved_to    move_self    create    delete    delete_self    filename
2            0             0        0             0                        1            1         0                     0                 0            0           0             0            /usr/local/nginx/html/

参数:

-r | --recursive:递归监视统计指定目录,即包括所有子目录。
-z | --zero:即使是未触发(事件统计为0的)的事件也输出。
-a | --ascending <event>:按event事件的统计升序排序。
-t | --timeout <seconds>:在seconds秒数后退出。
Note:一些限制
在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制
max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)。
max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数。
max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。
如:/root/tmp目录内有10个文件
[root@demo ~]# echo 5 > /proc/sys/fs/inotify/max_user_watches
[root@demo ~]# inotifywait -mr /root/tmp
Setting up watches.    Beware: since -r was given, this may take a while!
Failed to watch /root/tmp; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.
[root@demo ~]# echo 3 > /proc/sys/fs/inotify/max_user_instances
[root@demo ~]# inotifywait -mr /root/tmp &
[root@demo ~]# inotifywait -mr /root/tmp
Couldn't initialize inotify.    Are you running Linux 2.6.13 or later, and was the
CONFIG_INOTIFY option enabled when your kernel was compiled?    If so,
something mysterious has gone wrong.    Please e-mail rohan@mcgovern.id.au
and mention that you saw this message.
[root@demo tmp]# echo 1 > /proc/sys/fs/inotify/max_queued_events
[root@demo tmp]# ll 1 2 3
-rw-r--r-- 1 root root 0 12-08 04:48 1
-rw-r--r-- 1 root root 0 12-08 04:48 2
-rw-r--r-- 1 root root 0 12-08 04:48 3
[root@demo tmp]# cat 1 2 3 #先执行下面的命令,再执行这个测试
[root@demo ~]# inotifywait -mr /root/tmp
Setting up watches.    Beware: since -r was given, this may take a while!
Watches established.
/root/tmp/ OPEN 1
/root/tmp/ CLOSE_NOWRITE,CLOSE 1
Q_OVERFLOW
/root/tmp/ CLOSE_NOWRITE,CLOSE 2
/root/tmp/ OPEN 3
Q_OVERFLOW

当事件队列设置的较小时,队列溢出会出现上面提示(红色字)

以上是基本使用实例,具体应用follow me。

转载于:https://blog.51cto.com/haoyun/1083267

inotify之文件系统事件监控使用入门相关推荐

  1. linux 高效的文件系统事件监控 内核级解析方案 inotify

    转载 http://www.lvtao.net/config/inotify.html linux 高效的文件系统事件监控 内核级解析方案 inotify 安装inotify-tools (http: ...

  2. linux流行开源监控框架,Inotify: 高效、实时的Linux文件系统事件监控框架

    概要 - 为什么需要监控文件系统? 在日常工作中,人们往往需要知道在某些文件(夹)上都有那些变化,比如: 通知配置文件的改变 跟踪某些关键的系统文件的变化 监控某个分区磁盘的整体使用情况 系统崩溃时进 ...

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

    标签:inotifywait 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://cqfish.blog.51cto.com/6222 ...

  4. Inotify与Android文件监控FileObserver原理

    Inotify: 高效.实时的Linux文件系统事件监控框架 概要 - 为什么需要监控文件系统? 在日常工作中,人们往往需要知道在某些文件(夹)上都有那些变化,比如: 通知配置文件的改变 跟踪某些关键 ...

  5. linux inotify 监控文件系统事件

    1. Inotify 机制概述 1.1. Inotify 介绍 在日常的运维过程中,经常需要备份某些文件,或者对系统的某些文件进行监控,比如重要的配置文件等.如果需要作到实时同步或者监控,就需要使用内 ...

  6. Python Inotify 监视LINUX文件系统事件

    Inotify 可以监视的LINUX文件系统事件包括: --IN_ACCESS,即文件被访问  --IN_MODIFY,文件被write  --IN_ATTRIB,文件属性被修改,如chmod.cho ...

  7. inotify事件监控

    一.原理: inotify事件监控,可以监控文件系统中添加,删除,修改,移动等各种事件,一旦发现数据彼此不同,会通知rsync同步推送数据.inotify + rsync可以时时同步 二.准备工作 1 ...

  8. Python Watchdog——监控文件系统事件

    文章目录 简介 安装 初试 重定向到日志中 Handler类型 命令行工具 动态日志监控 遇到的坑 参考文献 简介 Watchdog是一款用于监控文件系统事件的Python库,对不同平台的事件进行了封 ...

  9. MacOS 开发 - FSEventStream(文件系统改变事件监控)

    文章目录 file system events 的构成 核心方法 Demo 地址 使用 1.导入 `CoreServices` 框架 2.添加属性 3.申明 `fsevents_callback` 方 ...

最新文章

  1. winform - FixedDialog
  2. struts2 ognl 判断数据类型_新华三攻防系列之防护篇从防护角度看Struts2历史漏洞...
  3. 【消息中间件】RabbitMQ 高级特性与应用问题
  4. 苏州大学计算机考研复试经验,苏州大学计算机考研复试经验总结.docx
  5. 丑憨批的爬虫笔记1(导学+requests))
  6. python cnn模型_ZfNet解卷积:可视化CNN模型( PythonCode可视化Cifar10)
  7. tomcat 优化_浅谈Tomcat服务器优化方法
  8. M1 mac 使用docker 安装mysql
  9. 第八章应用安全工程备考要点及真题分布
  10. Jetson AGX Xavier配置cuDNN流程
  11. 4999元起!iQOO 9 Pro赛道版上架:迄今为止成本最高手机后盖
  12. 库克看下!罗永浩深夜再谈收购苹果:还需要点时间
  13. torch.nn与torch.nn.functional
  14. java开花_那是花开-javaweb开发-51CTO博客
  15. linux系统及编程基础唐晓君,Linux-Shell编程之判断文件类型
  16. Vulkan_Ray Tracing 09_反射
  17. 微信小程序在组件中刷新当前页面
  18. 解决adb shell root权限
  19. PHPword 表格内换行处理
  20. clickhouse连接Tableau

热门文章

  1. 计算机基本运行方式,我今天才知道的电脑运行方式,你知道吗?
  2. php 字符串0转换bool_PHP数据类型转换(转)
  3. Golang——变量和常量详解
  4. ril.java_RIL.java里request流程
  5. mesh和wifi中继的区别_深度解读Mesh路由和无线中继的差异,谁才是性价比之选?...
  6. java黄金分割点游戏_结对编程1——黄金点小游戏项目简介及需求分析
  7. python做一个考试系统_Python在线考试系统防作弊功能的思路和实现
  8. 产品经理最痛苦的事情
  9. 错过SaaS,就是错过这个时代
  10. 2021年,年薪50W,这是产品经理的新出路?(接着发红包)