日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

inotify之文件系统事件监控使用入门

發(fā)布時(shí)間:2025/3/15 windows 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 inotify之文件系统事件监控使用入门 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
inotify是linux文件系統(tǒng)事件監(jiān)控機(jī)制,功能強(qiáng)大,控制簡(jiǎn)單,可以實(shí)現(xiàn)很多有用的功能。如:當(dāng)一個(gè)文件被訪問(wèn)、打開(kāi)、關(guān)閉、移動(dòng)、刪除等等時(shí)做一些處理。此功能需要內(nèi)核支持,從kernel 2.6.13開(kāi)始正式并入內(nèi)核,RHEL5已經(jīng)支持。
查看系統(tǒng)是否支持此功能:[root@demo ~]# ls -l /proc/sys/fs/inotify
總計(jì) 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目錄并下面有這三個(gè)文件,說(shuō)明內(nèi)核支持此功能
要想使用此功能還需要一個(gè)工具(inotify-tools),來(lái)控制內(nèi)核的這種功能,就是內(nèi)核的具體實(shí)現(xiàn)。工具下載地址: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安裝完畢,默認(rèn)安裝到/usr/local,可以通過(guò)./configure --prefix=PATH更改
工具集介紹:一共安裝了2個(gè)工具(命令),即inotifywait和inotifywatchinotifywait:在被監(jiān)控的文件或目錄上等待特定文件系統(tǒng)事件(open、close、delete等)發(fā)生,執(zhí)行后處于阻塞狀態(tài),適合在shell腳本中使用。inotifywatch:收集被監(jiān)視的文件系統(tǒng)使用度統(tǒng)計(jì)數(shù)據(jù),指文件系統(tǒng)事件發(fā)生的次數(shù)統(tǒng)計(jì)。
inotifywait使用例子:監(jiān)視web根目錄,用瀏覽器訪問(wèn)時(shí)出現(xiàn)下列事件[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參數(shù):-m | --monitor:一直監(jiān)視指定目錄,如果沒(méi)有這個(gè)選項(xiàng),inotifywait在接收到一個(gè)事件后就退出了。-r |?--recursive:遞歸監(jiān)視指定目錄,即包括所有子目錄。-q |?--quiet:輸出少量信息(只輸出事件信息,無(wú)附加開(kāi)頭的說(shuō)明信息)
inotofywatch使用例子:監(jiān)視統(tǒng)計(jì)web根目錄,用瀏覽器訪問(wèn)時(shí)出現(xiàn)下列事件[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/參數(shù):-r |?--recursive:遞歸監(jiān)視統(tǒng)計(jì)指定目錄,即包括所有子目錄。-z |?--zero:即使是未觸發(fā)(事件統(tǒng)計(jì)為0的)的事件也輸出。-a |?--ascending <event>:按event事件的統(tǒng)計(jì)升序排序。-t |?--timeout?<seconds>:在seconds秒數(shù)后退出。
Note:一些限制在/proc/sys/fs/inotify目錄下有三個(gè)文件,對(duì)inotify機(jī)制有一定的限制max_user_watches:設(shè)置inotifywait或inotifywatch命令可以監(jiān)視的文件數(shù)量(單進(jìn)程)。max_user_instances:設(shè)置每個(gè)用戶可以運(yùn)行的inotifywait或inotifywatch命令的進(jìn)程數(shù)。max_queued_events:設(shè)置inotify實(shí)例事件(event)隊(duì)列可容納的事件數(shù)量。如:/root/tmp目錄內(nèi)有10個(gè)文件[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 #先執(zhí)行下面的命令,再執(zhí)行這個(gè)測(cè)試[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當(dāng)事件隊(duì)列設(shè)置的較小時(shí),隊(duì)列溢出會(huì)出現(xiàn)上面提示(紅色字)
以上是基本使用實(shí)例,具體應(yīng)用follow me。

轉(zhuǎn)載于:https://blog.51cto.com/haoyun/1083267

總結(jié)

以上是生活随笔為你收集整理的inotify之文件系统事件监控使用入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。