Inotifywait 大目录
在 inotifywait man 中更改了以下内容
-r, --recursive 监视作为参数传递的任何目录的所有子目录。手表将被递归地设置到无限 深度。不遍历符号链接。新创建的 子目录也将被监视。
警告:如果您在观看根目录时使用此选项 一棵大树,可能需要相当长的时间才能看到所有 inotify 手表 已建立,此时将不会收到事件。 另外,由于每个子目录都会建立一个 inotify watch,因此 可能是最大金额 将达到每个用户的 inotify 手表数。默认最大值为8192;可以通过写信来增加 /proc/sys/fs/inotify/max_user_watches。
我认为这意味着每次调用 inotifywait 时,大型目录都会出现延迟。因此,像这样使用监视函数不断监视大目录
inotifywait -m /home/user/Documents
比像这样手动循环目录(来自手册页中的示例)更有效,
while inotifywait /home/user/Documents; do
#Do Something for each file change
done
因为 while 循环的每次迭代都必须再次设置 inotifywait 。但是使用第一个选项,我无法根据返回执行。理想情况下,我想要的是一个像这样的回调函数,
inotifywait -m --callback ./callback.sh /home/user/Documents
每次调用 callback.sh
时都会返回 inotifywait
的返回值。我将如何实施这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样进行管道传输:
请记住,某些操作会收到许多事件,每个事件都会触发脚本的启动。
您还可以使用 perl 或其他语言直接使用 API,这为您提供了极大的灵活性。
You can pipe it like:
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.