1.檢視是否支援inotify
ls -l /proc/sys/fs/inotify/
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL3gzN5IDNyETM0AjNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.檢查是否安裝
rpm -qa inotify-tools
3.安裝epol源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
4.檢查是否安裝epel源
ll /etc/yum.repos.d/epel.repo
若沒安裝,執行下面指令下載下傳到本地
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
5.安裝指令
yum install inotify-tools -y
離線安裝:
wget http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify
make
make install
6.監控路徑/home/kenick/logs下的ymhc_purchaseorder.*xlsx,星号為通配符号,如果有檔案建立或修改,就執行另一個腳本test.sh(列印資訊)。
#/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="/home/kenick/logs"
MSG="inotify.log"
PATTERN="ymhc_purchaseorder.*xlsx" # only when the rpm files changed.
while /usr/local/inotify/bin/inotifywait -e modify -e create -e move \
${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} " && \
./test.sh
done
測試:
6.1.先執行該腳本
生産環境注意使用nohup方式執行
nohup inotify.sh &
6.2.在對應路徑下複制符合ymhc_purchaseorder.*xlsx規則的檔案。
6.3.當路徑下檔案發生變化後,列印了相關資訊和執行了test.sh腳本。
7.叢集伺服器檔案同步
叢集伺服器中的每一個節點,都有可能在指定路徑下産生特定檔案,需要及時的把該檔案同步到其它節點上。
注意事項:a、由于同步檔案到其它節點上,也會觸發inotify監控,要防止重複傳播;b、有時同步可能耗時較長,監控和同步檔案要分開來做 b、注意監控最好設定一個間隔,不要消耗過多的伺服器資源。
監控腳本如下:
#/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="/home/order/app/files"
MSG=".inotify"
DATA_MSG="inotify_purchaseorder.log"
PATTERN="ymhc_purchaseorder.*xlsx"
DIR_PATH="/home/order/app/files/"
HOST_ARRAY=(xxx.xxx.xxx.xxx:22 xxx.xxx.xxx.xxx:22)
PID=$$
while /usr/local/inotify/bin/inotifywait -e create \
${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}' `
time=$(date "+%Y-%m-%d %H:%M:%S")
FILE_NAME="${EVENTPATH}/${FILE:5}"
if [ ! -z ${FILE} ];then
flagFile="${EVENTPATH}/.inotify.${FILE}"
# file not exist
if [ ! -f "$flagFile" ]; then
echo $$ >> ${flagFile}
echo "${time} pid:${PID} ${FILE_NAME} changed,action:${ACTION}" >> ${EVENTPATH}/${DATA_MSG}
#execute sync
for HOST in ${HOST_ARRAY[@]}
do
./lftp_upload.sh ${HOST} ${DIR_PATH} ${flagFile}
./lftp_upload.sh ${HOST} ${DIR_PATH} ${FILE_NAME} &
done
fi
fi
sleep 1
done
lftp傳輸檔案腳本如下
#/bin/bash
time=$(date "+%Y-%m-%d %H:%M:%S")
DIR_PATH="/home/order/app/files/"
log="/home/order/app/files/inotify_purchaseorder.log"
HOST=$1
DIR_PATH=$2
FILE_NAME=$3
PID=$$
echo "${time} ${PID} start upload ${FILE_NAME} to ${HOST}" >> ${log}
lftp -u 使用者名,密碼 sftp://${HOST} << EOM
cd $DIR_PATH
put ${FILE_NAME}
bye
EOM
echo "${time} ${PID} start upload ${FILE_NAME} to ${HOST} end!" >> ${log}