马丁传奇

导航

inotifywait命令

【命令格式】: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
【命令原意】: inote file system wait
【命令路径】:
【命令功能】: 等待所监听的文件系统触发操作事件
【执行权限】: root
【命令描述】:
    众所周知,Linux 桌面系统与 MAC 或 Windows 相比有许多不如人意的地方,为了改善这种状况,开源社区提出用户态需要内核提供一些机制,以便用户态能够及时地得知内核或底层硬件设备发生了什么,从而能够更好地管理设备,给用户提供更好的服务,如 hotplug、udev 和 inotify 就是这种需求催生的。Hotplug 是一种内核向用户态应用通报关于热插拔设备一些事件发生的机制,桌面系统能够利用它对设备进行有效的管理,udev 动态地维护 /dev 下的设备文件,inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知,该机制是著名的桌面搜索引擎项目 beagle 引入的,并在 Gamin 等项目中被应用。

    Inotify 是一个内核用于通知用户空间程序文件系统变化的机制,是基于inode级别的文件系统监控技术,是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能,内核要求2.6.13以上,inotify能监控非常多的文件系统事件,通过监控这些事件来监控文件是否发生变更,然后通过rsync来更新发生变更的文件,Inotify 可以监视的文件系统事件包括:

【常用选项】:
    -h 或 --help 显示帮助

    @<file> Exclude the specified file from being watched.

    --exclude <pattern> 指定排除(忽略)部分文件或目录,这些文件或目录上不监听任何事件,正则表达式书写(相对路径)
      FMT: # --exclude 'Runtime' //忽略对Runtime目录的事件监听

    --excludei <pattern> 与--exclude相同,区别是此选项正则表达式忽略大小写

    -m 或 --monitor 持续保持监听(如果不加此选项,则监听到一次后便退出)

    -d 或 --daemon 以守护进程方式后台运行(除了在后台运行外,与-m选项一样)

    -r 或 --recursive 递归监听其下所有子目录及文件

    --fromfile <file> Read files to watch from <file> or `-' for stdin.

    -o 或 --outfile <file> 将事件输出到指定文件,而不输出到屏幕
      FMT: -o /var/log/inotifywait.log

    -s 或 --syslog 将错误发送到系统日志,而不是输出到屏幕

    -q 或 --quiet 打印较少信息(仅打印事件)

    -qq 不打印任何信息(静默方式)

    --format <fmt> 设置打印屏幕的格式,常见选项:%T时间;%w触发事件文件所在绝对路径;%f触发事件文件名称;%e触发的事件名称;
      FMT: # --format '%T %f %e'

    --timefmt <fmt> 指定输出时间内容,相当于将时间赋值给%T
      FMT: # --timefmt '%y-%m-%d %H:%M'

    -c 或 --csv 用CSV格式打印事件

    -t 或 --timeout <seconds> 指定一次性监听时间,超时退出监听(值为0表示永不超时,单位:秒),不可与 -m -d 连用
      FMT: # -t 60 //设定监听60秒,60秒内监听到事件立即退出,如果监听不到事件60秒后也退出

    -e 或 --event <event1> [ -e|--event <event2> ... ] 指定要监听的事件(多个事件用逗号分割)
      FMT: # -e 'create,delete,close_write,attrib,moved_to'

【总结提示】:

Exit status: 退出返回状态值
  0 - An event you asked to watch for was received.
      正常收到监听事件

  1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred.
      意外收到一个未监听的事件(如:文件自删除,umount及其它情况)

  2 - The --timeout option was given and no events occurred in the specified interval of time.
      当给定了超时选项,设定时间内没有事件产生

Events: 事件
  access        file or directory contents were read 文件或目录被(访问)读取r
  modify        file or directory contents were written 文件或目录被写入w
  attrib        file or directory attributes changed 文件或目录属性变更【理由:chmod更改属性】
  close_write   file or directory closed, after being opened in writeable mode 文件或目录被写关闭【理由:文件内容被更改】
  close_nowrite file or directory closed, after being opened in read-only mode 文件或目录以只读方式打开后关闭
  close         file or directory closed, regardless of read/write mode 文件或目录被用编辑器(不管是读或写)关闭
  open          file or directory opened 文件或目录被用编辑器打开
  moved_to      file or directory moved to watched directory 文件或目录被移动进来【理由:mv目录内重命名】
  moved_from    file or directory moved from watched directory 文件或目录被移动出去
  move          file or directory moved to or from watched directory 文件或目录不管是移出或移进
  create        file or directory created within watched directory 文件或目录被创建【理由:mkdir创建目录】
  delete        file or directory deleted within watched directory 文件或目录被删除【理由:rm删除】
  delete_self   file or directory was deleted 文件或目录自删除
  unmount       file system containing file or directory unmounted 文件系统取消挂载

【参考文档】:

【选项举例】:

★ 监听/wwwroot/web.d目录内所有文件和目录的"增删改"操作

[root@zzx ~]# /usr/local/inotify/bin/inotifywait -mrq -e 'create,delete,close_write,attrib,moved_to' --timefmt '%Y-%m-%d %H:%M' --format '%T %f %e' /wwwroot/web.d/
2014-11-27 17:34 text.txt CREATE
2014-11-27 17:34 text.txt CLOSE_WRITE,CLOSE
2014-11-27 17:34 ttt.t MOVED_TO
2014-11-27 17:35 bb DELETE

 

posted on 2014-11-27 18:06  马丁传奇  阅读(10707)  评论(1编辑  收藏  举报