文章目錄
Linux內核從2.6.13開始,引入了inotify機制。通過intofity機制,能夠對文件系統的變化進行監控,如對文件進行創建、刪除、修改等操作,可以及時通知應用程序進行相關事件的處理。這種響應處理機制,避免了頻繁的文件輪詢任務,提高了任務的處理效率。
一、檢查系統內核版本
[root@localhost tan]# uname -r
2.6.32-696.el6.x86_64
二、檢查系統是否支持inotify
[root@localhost tan]# ls -lsart /proc/sys/fs/inotify
total 0
0 dr-xr-xr-x 0 root root 0 Jan 17 08:47 ..
0 dr-xr-xr-x 0 root root 0 Jan 17 08:53 .
0 -rw-r--r-- 1 root root 0 Jan 19 13:33 max_user_watches
0 -rw-r--r-- 1 root root 0 Jan 19 13:33 max_queued_events
0 -rw-r--r-- 1 root root 0 Jan 19 13:40 max_user_instances
[root@localhost tan]#
如果出現上面結果說明系統支持inotify。
三、下載安裝(下載有點慢)
[root@localhost tan]#wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@localhost tan]# tar -zvxf inotify-tools-3.14.tar.gz
[root@localhost tan]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install
四、查看inotify默認參數
[root@localhost bin]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384
[root@localhost bin]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
fs.epoll.max_user_watches = 798863
[root@localhost bin]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
五、修改inotify參數
1、命令修改
[root@localhost bin]# sysctl -w fs.inotify.max_user_instances=130
fs.inotify.max_user_instances = 130
2、文件修改
[root@localhost]# vi /etc/sysctl.conf
#添加如下代碼
fs.inotify.max_user_instances=130
3、參數說明
- max_user_instances:每個用戶創建inotify實例最大值
- max_queued_events:inotify隊列最大長度,如果值太小,會出現錯誤,導致監控文件不准確
- max_user_watches:要知道同步的文件包含的目錄數,可以用:
[root@localhost]# find /home/rain -type d|wc -l 統計,必須保證參數值大於統計結果(/home/tan/uploadFile/為同步文件目錄)。
六、創建實時監控腳本 (file 里面放的需要監聽的目錄)
[root@localhost shell]# vi inotify.sh
/usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete --fromfile '/root/shell/file' --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --outfile '/home/tan/inotify.log'
[root@localhost shell]# vi file
/home/tan
@/home/tan/uploadFile
inotifywait常用參數:
- –timefmt 時間格式
- %y年 %m月 %d日 %H小時 %M分鍾
- –format 輸出格式
- %T時間 %w路徑 %f文件名 %e狀態
- -m 始終保持監聽狀態,默認觸發事件即退出。
- -r 遞歸查詢目錄
- -q 打印出監控事件
- -e 定義監控的事件,可用參數:
- open 打開文件
- access 訪問文件
- modify 修改文件
- delete 刪除文件
- create 新建文件
- attrb 屬性變更
事件 描述
access 訪問,讀取文件。
modify 修改,文件內容被修改。
attrib 屬性,文件元數據被修改。
move 移動,對文件進行移動操作。
create 創建,生成新文件
open 打開,對文件進行打開操作。
close 關閉,對文件進行關閉操作。
delete 刪除,文件被刪除。
七:實例操作
1、首先啟動監聽腳本,權限問題的話先:chmod 755 inotify.sh
[root@localhost shell]# ./inotify.sh
我在監聽的目錄中上傳文件,沒截圖,自己想象吧
[root@localhost tan]# cat inotify.log
18-01-19 15:07 /home/tan/uploadfile/test//ssh.txt CREATE
18-01-19 15:13 /home/tan/uploadfile/test/QQ20180119143826.png CREATE
這里可以看到打印的日志了。
八、附
1、inotifywait
使用方法和參數說明: 使用命令help就行
[root@localhost bin]# ./inotifywait -h
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
2、inotifywatch
使用方法和參數說明:
[root@localhost bin]# ./inotifywatch -h
inotifywatch 3.14
Gather filesystem usage statistics using inotify.
Usage: inotifywatch [ options ] file1 [ file2 ] [ ... ]
Options:
-h|--help Show this help text.
-v|--verbose Be verbose.
@<file> Exclude the specified file from being watched.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
--exclude <pattern>
Exclude all events on files matching the extended regular
expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-z|--zero
In the final table of results, output rows and columns even
if they consist only of zeros (the default is to not output
these rows and columns).
-r|--recursive Watch directories recursively.
-t|--timeout <seconds>
Listen only for specified amount of time in seconds; if
omitted or 0, inotifywatch will execute until receiving an
interrupt signal.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
-a|--ascending <event>
Sort ascending by a particular event, or `total'.
-d|--descending <event>
Sort descending by a particular event, or `total'.
Exit status:
0 - Exited normally.
1 - Some error occurred.
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
常用事件
CREATE
,創建MODIFY
,修改CLOSE_WRITE,CLOSE
,寫入成功
原文出處:1felse -> http://www.1felse.com/blog/view.do?blogId=ff8080816102bdbb01610d45232d005a