Using inotify in a script to monitor a directory

无人久伴 提交于 2019-12-06 12:07:24

问题


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

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?


回答1:


You are not far away from solution. If you want to use inotifywait in your while statement you should not use -m option. With this option inotifywait never end because it's the monitor option. So you never go into the while.

This should work :

#!/bin/sh

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



回答2:


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


来源:https://stackoverflow.com/questions/24567608/using-inotify-in-a-script-to-monitor-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!