歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux利用inotify-tools的inotifywait實現:當文件夾內容改變時自動執行一段腳本

Linux利用inotify-tools的inotifywait實現:當文件夾內容改變時自動執行一段腳本

日期:2017/3/1 14:49:14   编辑:關於Linux
Linux利用inotify-tools的inotifywait實現:當文件夾內容改變時自動執行一段腳本 當我在建一個rpm包管理服務器時,裡面有個這樣的要求,要求當有新的rpm存入指定目錄時,自動執行一段腳本去對這個rpm包進行檢測。 這裡利用了inotify-tools的inotifywait的模塊,裡面有個事件處理的參數-e,見它的手冊。 我的代碼如下: [python] #/bin/bash ################################################################ # automatically run a script(action.sh) when the contents # of a directory (${EVENTPATH}) changed. # pls. install the inotify-tools-3.13-1.el4.rf.i386.rpm module # before use this scripts # Aborn Jiang ([email protected]) # Sep.8, 2013 ################################################################ EVENTPATH="." MSG=".inotifymsg" PATTERN=".rpm$" # only when the rpm files changed. while inotifywait -e modify -e create -e delete -e moved_to -e moved_from \ ${EVENTPATH} 1>${EVENTPATH}/${MSG} 2>/dev/null; do FILE=`cat $EVENTPATH/${MSG} |egrep ${PATTERN} | awk '{print $3}' ` ACTION=`cat $EVENTPATH/${MSG} |egrep ${PATTERN} | awk '{print $2}' ` [ ! -z ${FILE} ] && \ echo "in the directory ${EVENTPATH}, the file: ${FILE} modified, action:${ACTION} " && \ ./action.sh done 我這裡是只檢測是不是有rpm變動,你可以修改代碼裡的PATTERN變量內容,就可以檢測任何文件!如果你要檢測任何文件,你可以把PATTERN設置為“*” 我的執行腳本放在action.sh裡
Copyright © Linux教程網 All Rights Reserved