在脚本中使用inotify监视目录 [英] Using inotify in a script to monitor a directory

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

问题描述

我已经编写了一个bash脚本来监视特定目录"/root/secondfolder/",该脚本如下:

I have written a bash script to monitor a particular directory "/root/secondfolder/" the script is as follows:

#!/bin/sh

while inotifywait -mr -e close_write "/root/secondfolder/"
do
    echo "close_write"
done

当我在"/root/secondfolder/"中创建一个名为"fourth.txt"的文件并将其写入内容,保存并关闭时,它会输出以下内容,但不会回显"close_write":

When I create a file called "fourth.txt" in "/root/secondfolder/" and write stuff to it, save and close it, it outputs the following but it does not echo "close_write":

/root/secondfolder/ CLOSE_WRITE,CLOSE fourth.txt

有人可以指出我正确的方向吗?

can someone point me in the right direction?

推荐答案

原来,我要做的就是将命令传递到while循环中:

It turns out all I had to do was pipe the command into a while loop:

!/bin/sh

inotifywait -mqr -e close_write "/root/secondfolder/" | while read line
do
echo "close_write"
done

这篇关于在脚本中使用inotify监视目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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