大型目录的Inotifywait [英] Inotifywait for large directories

查看:173
本文介绍了大型目录的Inotifywait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


-r,--recursive查看作为参数传递的任何目录的所有子目录。手表将递归设置为无限
的深度。符号链接没有传递。



警告:如果在查看
a大树的根目录时使用此选项,它可能需要一段时间,直到所有inotify手表
建立
,并且在这个时候将不会收到事件。
此外,由于每个子目录将建立一个inotify手表,因此
可能达到每个用户的inotify手表的最大金额
。默认最大值为8192;它可以通过写入
/ proc / sys / fs / inotify / max_user_watches增加。


每次调用 inotifywait 时,大目录都会有延迟。因此,不断地监视具有监视功能的大目录,如

  inotifywait -m / home / user / Documents 

比手动循环遍历目录更有效率(从手册页中的示例)

 ,而inotifywait / home / user / Documents; do 
#Do每个文件更改的内容
done

while循环你必须再次设置inotifywait。但是使用第一个选项,我不能基于返回执行。我想要的是一个回调函数,像这样

  inotifywait -m --callback ./callback.sh / home / user /文件

因此 callback.sh 时间,返回值 inotifywait


b b b b b b b b b pre> inotifywait -m / my / directory |同时读LINE; do ./do_something.sh $ LINE; done

请记住,某些操作会收到很多事件,你的脚本。



您还可以使用perl或其他某种语言直接使用API​​,这给您很大的灵活性。


In the inotifywait man changes the following is stated

-r, --recursive Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not tra‐versed. Newly created subdirectories will also be watched.

Warning: If you use this option while watching the root directory of a large tree, it may take quite a while until all inotify watches are established, and events will not be received in this time. Also, since one inotify watch will be established per subdirectory, it is possible that the maximum amount of inotify watches per user will be reached. The default maximum is 8192; it can be increased by writing to /proc/sys/fs/inotify/max_user_watches.

I take this to mean that every time inotifywait is called, there is a delay for large directories. Therefore, constantly monitoring a large directory with monitor function like so

inotifywait -m /home/user/Documents

is more efficient than manually looping through the directory like so (from an example in the man pages)

while inotifywait /home/user/Documents; do
 #Do Something for each file change
done

as every iteration of the while loop you have to set up inotifywait again. But with the first option, I can't execute based on the return. Ideally what I want is a callback function like so

inotifywait -m --callback ./callback.sh /home/user/Documents

so callback.sh gets called each time with the return value of inotifywait. How would I implement this?

解决方案

You can pipe it like:

inotifywait -m /my/directory | while read LINE; do ./do_something.sh $LINE; done

Keep in mind that you get many events for certain operations, each of which will trigger the launch of your script.

You can also use perl or some other language to use the API directly, which gives you tons of flexibility.

这篇关于大型目录的Inotifywait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆