sai*_*dhu 32 linux linux-kernel
我是dnotify/inotify命令的新手.任何人都可以帮助我编写一个脚本,以便它持续监视一个目录,并指出它有一些变化或修改.
thn*_*nee 30
Inotify本身是一个可通过来自例如C程序的调用访问的内核模块. http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/
有一个名为inotify-tools的应用程序套件,其中包含:
inotifywait - 等待使用inotify更改文件
和
inotifywatch - 使用inotify收集文件系统访问统计信息
您可以直接从命令行使用inotify,例如像这样连续监视主目录下的所有更改(可能会生成大量输出):
inotifywait -r -m $HOME
Run Code Online (Sandbox Code Playgroud)
这是一个脚本,它持续监视并响应从日志活动的man文件中复制的Apache日志活动:
#!/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
Run Code Online (Sandbox Code Playgroud)
Ala*_*ile 12
下面是我用来查看单个文件的操作."-m"仅在一个事件后导致监视与退出.要获取时间戳,您至少需要3.13版本的inotify-tools,但如果这不重要(或者在您的操作系统上不可用或难以更新),您可以跳过timefmt和format选项.另一个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
Run Code Online (Sandbox Code Playgroud)
inotifywait也有监视目录的选项,因此请查看联机帮助页.添加-r用于递归以监视目录的子项.
这是一个示例,我在不同的窗口中键入的命令显示为" - >"前缀:
$ 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
Run Code Online (Sandbox Code Playgroud)
在上面之后,我尝试重新制作tmpdir("mkdir tmpdir"),但没有输出.新的tmpdir与旧的tmpdir不是同一个目录.时间到^ C并停止itnotifywait.