Linux 如何使用 dnotify /inotify 命令持续监控目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7566569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:22:21  来源:igfitidea点击:

How to continuously monitor the directory using dnotify /inotify command

linuxlinux-kernel

提问by sai sindhu

I am new to dnotify/inotify command. Can any one help me how to write a script such that it continuously monitors a directory and indicates that there is some change or modification to it.

我是 dnotify/inotify 命令的新手。任何人都可以帮助我如何编写脚本,使其持续监视目录并指示对其进行了一些更改或修改。

采纳答案by thnee

Inotify itself is a kernel module accesible via calls from e.g. a C program. http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/

Inotify 本身是一个内核模块,可通过来自例如 C 程序的调用访问。 http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/

There is an application suite called inotify-tools, which contains:

有一个名为 inotify-tools 的应用程序套件,其中包含:

inotifywait - wait for changes to files using inotify

http://linux.die.net/man/1/inotifywait

inotifywait - 使用 inotify 等待对文件的更改

http://linux.die.net/man/1/inotifywait

and

inotifywatch - gather filesystem access statistics using inotify

http://linux.die.net/man/1/inotifywatch

inotifywatch - 使用 inotify 收集文件系统访问统计信息

http://linux.die.net/man/1/inotifywatch

You can use inotify directly from command line, e.g. like this to continuously monitor for all changes under home directory (may generate lots of output):

您可以直接从命令行使用 inotify,例如像这样连续监视主目录下的所有更改(可能会生成大量输出):

inotifywait -r -m $HOME

And here is a script that monitors continuously and reacts to Apache log activity, copied from the man file of inotifywait:

这是一个脚本,它持续监控并响应 Apache 日志活动,从 inotifywait 的 man 文件中复制:

#!/bin/sh
while inotifywait -e modify /var/log/messages; do
  if tail -n1 /var/log/messages | grep httpd; then
    kdialog --msgbox "Apache needs love!"
  fi
done

回答by Alan Carwile

Below is what I use to see operations on an individual file. "-m" causes monitoring vs. exit after just one event. To get timestamps, you need at least 3.13 version of inotify-tools, but if that is not important (or not available on your OS or hard to update to) you can skip the timefmt and format options. "cat /etc/resolv.conf" in another shell leads to the results below:

下面是我用来查看对单个文件的操作的内容。“-m”仅在一个事件后导致监视与退出。要获取时间戳,您至少需要 3.13 版本的 inotify-tools,但如果这不重要(或在您的操作系统上不可用或难以更新),您可以跳过 timefmt 和格式选项。另一个shell中的“cat /etc/resolv.conf”导致以下结果:

$ inotifywait -m --timefmt '%H:%M' --format '%T %w %e %f' /etc/resolv.conf

Setting up watches.  
Watches established.
12:49 /etc/resolv.conf OPEN 
12:49 /etc/resolv.conf ACCESS 
12:49 /etc/resolv.conf CLOSE_NOWRITE,CLOSE 

inotifywait has options for monitoring directories as well, so check the manpage. Add -r for recursive to monitor children of a dir.

inotifywait 也有用于监视目录的选项,因此请查看联机帮助页。添加 -r 用于递归以监视目录的子项。

Here's an example with the commands I typed in a different window shown with "->" prefix:

这是我在以“->”前缀显示的不同窗口中键入的命令的示例:

$ inotifywait -mr --timefmt '%H:%M' --format '%T %w %e %f' /home/acarwile/tmpdir
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

-> cd into directory, no info
-> ls in directory
13:15 /home/acarwile/tmpdir/ OPEN,ISDIR 
13:15 /home/acarwile/tmpdir/ CLOSE_NOWRITE,CLOSE,ISDIR 

-> touch newfile
13:16 /home/acarwile/tmpdir/ CREATE newfile
13:16 /home/acarwile/tmpdir/ OPEN newfile
13:16 /home/acarwile/tmpdir/ ATTRIB newfile
13:16 /home/acarwile/tmpdir/ CLOSE_WRITE,CLOSE newfile

-> mv newfile renamedfile
13:16 /home/acarwile/tmpdir/ MOVED_FROM newfile
13:16 /home/acarwile/tmpdir/ MOVED_TO renamedfile

-> echo hello >renamedfile
13:16 /home/acarwile/tmpdir/ MODIFY renamedfile
13:16 /home/acarwile/tmpdir/ OPEN renamedfile
13:16 /home/acarwile/tmpdir/ MODIFY renamedfile
13:16 /home/acarwile/tmpdir/ CLOSE_WRITE,CLOSE renamedfile

-> touch renamedfile
13:17 /home/acarwile/tmpdir/ OPEN renamedfile
13:17 /home/acarwile/tmpdir/ ATTRIB renamedfile
13:17 /home/acarwile/tmpdir/ CLOSE_WRITE,CLOSE renamedfile

-> rm renamedfile
13:17 /home/acarwile/tmpdir/ DELETE renamedfile

-> cd ..; rmdir tmpdir
13:17 /home/acarwile/tmpdir/ DELETE_SELF 

After the above, I tried to remake the tmpdir ("mkdir tmpdir") but got no output from that. The new tmpdir is not the same directory as the old tmpdir. Time to just ^C and stop itnotifywait.

在上述之后,我尝试重新制作 tmpdir(“mkdir tmpdir”),但没有得到任何输出。新的 tmpdir 与旧的 tmpdir 目录不同。时间只是 ^C 并停止 itnotifywait。

回答by Wernight

As I said on https://superuser.com/a/747574/28782, I made a helper script that uses inotifywaitwithout some of its direct limitations: inotifyexec

正如我在https://superuser.com/a/747574/28782 上所说的,我制作了一个使用inotifywait的辅助脚本,没有一些直接限制:inotifyexec

Usage example (supposing you've added it in your system path as executable):

用法示例(假设您已将其作为可执行文件添加到系统路径中):

inotifyexec "echo test" -r .