Inotify 是一個 Linux特性,它監控文件系統操做,好比讀取、寫入和建立。Inotify 反應靈敏,用法很是簡單,而且比 cron 任務的繁忙輪詢高效得多。學習如何將 inotify 集成到您的應用程序中,並發現一組可用來進一步自動化系統治理的命令行工具。更加詳細介紹查看百科介紹node
Linux內核從2.6.13開始,引入了inotify機制。經過intofity機制,可以對文件系統的變化進行監控,如對文件進行建立、刪除、修改等操做,能夠及時通知應用程序進行相關事件的處理。這種響應處理機制,避免了頻繁的文件輪詢任務,提升了任務的處理效率。linux
[root@node1 ~]# uname -a Linux node1 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@node1 ~]# ls -l /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Jun 24 01:30 max_queued_events -rw-r--r-- 1 root root 0 Jun 24 01:30 max_user_instances -rw-r--r-- 1 root root 0 Jun 24 01:30 max_user_watches
[root@node1 ~]# rpm -qa inotify-tools inotify-tools-3.14-1.el6.x86_64
出現以上命令代表已經安裝inofity,不然經過如下方式添加安裝員,在線安裝:express
[root@node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo [root@node1 ~]# rpm -y install inotify-tools
[root@node1 ~]# which inotifywait /usr/bin/inotifywait
[root@node1 ~]# inotifywait --help inotifywait 3.14 Wait for a particular event on a file or set of files. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern> Like --exclude but case insensitive. 排除文件或者目錄的時候不區分大小寫 -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. 始終保持事件監聽狀態 -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog.後臺運行 -r|--recursive Watch directories recursively. 遞歸查詢目錄 --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events).打印不多的信息,僅僅打印監控事件的信息 安靜狀態 -qq Print nothing (not even events). --format <fmt> Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt> strftime-compatible format string for use with %T in --format string.指定時間輸出的格式 -c|--csv Print events in CSV format. -t|--timeout <seconds> When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. 事件 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. 2 - The --timeout option was given and no events occurred in the specified interval of time. Events: access file or directory contents were read modify file or directory contents were written attrib file or directory attributes changed 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 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 delete file or directory deleted within watched directory delete_self file or directory was deleted unmount file system containing file or directory unmounted