inotify 是一個 Linux 內核特性,它監控文件系統,並且及時向專門的應用程序發出相關的事件警告,比如刪除、讀、寫和卸載操作等。inotify安裝完成之後會有兩個命令,
inotifywait 和 inotifywatch。inotifywait用於等待文件或者文件集上的一個特定事件,可以監控任何文件或者目錄位置,並且可以遞歸地監控整個目錄樹;inotifywatch 用於收集被監控的文件系統統計數據,包括每個inotify事件發生多少次等信息。
安裝
cd /tmp
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure&&make&&make install
ln -sv /usr/local/lib/libinotify* /usr/lib/
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
# vim /tmp/mon.sh
#!/bin/bash
src=/tmp/test/ # directory to monitor
/usr/local/bin/inotifywait -rmq -e modify $src | while read event
do
echo "hello" >> 1.txt
done
做成開機啓動
chmod u+x /tmp/mon.sh
echo "nohup /bin/bash /tmp/mon.sh &" >> /etc/rc.d/rc.local
nohup /bin/bash /tmp/mon.sh &
這時候只要/tmp/test/一有改動,在會觸發inotifywait,運行echo命令。