inotify之文件系统事件监控使用入门
生活随笔
收集整理的這篇文章主要介紹了
inotify之文件系统事件监控使用入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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和inotifywatchinotifywait:在被監控的文件或目錄上等待特定文件系統事件(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。
查看系統是否支持此功能:[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和inotifywatchinotifywait:在被監控的文件或目錄上等待特定文件系統事件(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之文件系统事件监控使用入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加缓存
- 下一篇: linux find 用法详解 + 实例