Inotifywait 大目录

发布于 2024-12-15 05:02:10 字数 926 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2024-12-22 05:02:10

您可以像这样进行管道传输:

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

请记住,某些操作会收到许多事件,每个事件都会触发脚本的启动。

您还可以使用 perl 或其他语言直接使用 API,这为您提供了极大的灵活性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文