mib*_*456 2 linux bash monitoring echo inotifywait
我写了一个bash脚本来监控一个特定的目录“/root/secondfolder/”,脚本如下:
#!/bin/sh
while inotifywait -mr -e close_write "/root/secondfolder/"
do
echo "close_write"
done
Run Code Online (Sandbox Code Playgroud)
当我在“/root/secondfolder/”中创建一个名为“fourth.txt”的文件并向其中写入内容,保存并关闭它时,它输出以下内容但不回显“close_write”:
/root/secondfolder/ CLOSE_WRITE,CLOSE fourth.txt
Run Code Online (Sandbox Code Playgroud)
有人可以指出我正确的方向吗?
您离解决方案不远了。如果要inotifywait在while语句中使用,则不应使用-m选项。这个选项inotifywait永远不会结束,因为它是monitor选项。所以你永远不会进入while.
这应该工作:
#!/bin/sh
while inotifywait -r -e close_write "/root/secondfolder/"
do
echo "close_write"
done
Run Code Online (Sandbox Code Playgroud)
事实证明,我所要做的就是将命令输入到 while 循环中:
!/bin/sh
inotifywait -mqr -e close_write "/root/secondfolder/" | while read line
do
echo "close_write"
done
Run Code Online (Sandbox Code Playgroud)