linux - 在脚本中使用 inotify 监控目录

标签 linux bash echo monitoring inotifywait

我写了一个bash脚本来监控特定目录“/root/secondfolder/”脚本如下:

#!/bin/sh

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

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

/root/secondfolder/ CLOSE_WRITE,CLOSE fourth.txt

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

最佳答案

您离解决方案不远了。如果您想在 while 语句中使用 inotifywait,则不应使用 -m 选项。使用此选项 inotifywait 永远不会结束,因为它是 monitor 选项。所以你永远不会进入 while

这应该有效:

#!/bin/sh

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

关于linux - 在脚本中使用 inotify 监控目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24567608/

相关文章:

c++ - 如何将 qlabel 文本更改为看起来像窗口标题文本

node.js - 从node.js调用sh脚本

php - PHP 中的回显表

linux - Git remote 尝试以我当前的本地用户身份连接到我的服务器

linux - 可以在 Git bash 中使用的 Linux 终端命令列表有哪些?

python - 添加用户的 bash 脚本

bash - Sed print with evaluated date command on back reference

php - 如何将 css 应用于 src 为 PHP 的 iframe?

ubuntu 服务器上的 laravel websockets 和 pusher-js CROS 问题

PHP 警告 : PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-zts-20131226/pdo_mysql.so'