Inotify 鏄枃浠剁郴緇熶簨浠剁洃鎺ф満鍒訛紝璁″垝鍖呭惈鍦ㄥ嵆灝嗗彂甯冪殑 Linux 鍐呮牳涓綔涓?dnotify 鐨勬湁鏁堟浛浠c俤notify 鏄緝鏃╁唴鏍告敮鎸佺殑鏂囦歡鐩戞帶鏈哄埗銆侷notify涓縐嶅己澶х殑銆佺粏綺掑害鐨勩佸紓姝ョ殑鏈哄埗錛屽畠婊¤凍鍚勭鍚勬牱鐨勬枃浠剁洃鎺ч渶瑕侊紝涓嶄粎闄愪簬瀹夊叏鍜屾ц兘銆備笅闈㈣鎴戜滑涓璧峰涔犲浣曞畨瑁?inotify 鍜屽浣曟瀯寤轟竴涓ず渚嬬敤鎴風┖闂村簲鐢ㄧ▼搴忔潵鍝嶅簲鏂囦歡緋葷粺浜嬩歡銆?/p>
1.1鍚屾宸ュ叿瀹夎
1銆佽緭鍏ュ懡浠わ細su root錛屽垏鎹㈠埌瓚呯駭鐢ㄦ埛銆?br />
2銆佸厛鏌ョ湅linux鐨勫唴鏍告槸鍚︽敮鎸乮notify錛屾敮鎸乮notify鐨勫唴鏍告渶灝忎負2.6.13錛岃緭鍏ュ懡浠わ細uname –a銆傚涓嬪浘鎵紺猴紝鍐呮牳涓?.6.27錛屾敮鎸乮notify錛?br />
娉細濡傛灉鍐呮牳浣庝簬2.6.13錛岃鍗囩駭鍐呮牳鎴栭噸鏂板畨瑁呭唴鏍哥増鏈洿楂樼殑linux緋葷粺銆?br />
3銆佸緩绔嬪悓姝sh淇′換鍏崇郴錛岃緭鍏ュ懡浠わ細cd $HOME錛岃繘鍏ョ敤鎴鋒牴鐩綍銆?br />
杈撳叆鍛戒護錛歴sh-keygen -t rsa (浼氬嚭鐜板嚑涓彁紺轟俊鎭紝涓鐩存寜鍥炶濺鍗沖彲)銆?br />
浼氬湪 cd $HOME/.ssh/鐩綍涓嬬敓鎴?涓枃浠秈d_rsa銆乮d_rsa.pub銆?br />
杈撳叆鍛戒護錛歝p id_rsa.pub authorized_keys錛屽皢id_rsa.pub鎷瘋礉鎴恆uthorized_keys銆?br />
灝嗘巿鏉冨瘑閽ュ垎鍙戝埌iEPG鏈嶅姟鍣?192.168.100.101)涓婏紝杈撳叆鍛戒護錛?br />
scp ~/.ssh/authorized_keys root@192.168.100.101:/root/.ssh/
濡傛灉鏈夊鍙頒笅杞芥湇鍔″櫒錛屾瘡鍙伴兘欏昏繍琛屼竴嬈′笂闈㈢殑瀵嗛挜涓嬪彂鍛戒護銆?br />
4銆侀氳繃濡備笅鍛戒護鏌ョ湅緋葷粺鏄惁鏀寔inotify錛歭l /proc/sys/fs/inotify
濡傛灉鏈夊涓嬭緭鍑猴紝琛ㄧず緋葷粺鍐呮牳宸茬粡鏀寔inotify錛?br />
total 0
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_queued_events
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_instances
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_watches
5銆佸彇寰楄蔣浠跺寘inotify-tools-3.13.tar.gz錛屾斁鍦?tmp涓嬨?br />
6銆佽緭鍏ュ懡浠わ細tar zvxf inotify-tools-3.13.tar.gz錛岃В鍘嬭蔣浠跺寘銆?br />
7銆佽緭鍏ュ懡浠わ細cd inotify-tools-3.13錛岃繘鍏ヨВ鍘嬪悗鐨勭洰褰曘?br />
8銆佽緭鍏ュ懡浠わ細./configure
9銆佽緭鍏ュ懡浠わ細make
10銆佽緭鍏ュ懡浠わ細make install
11銆佸湪緋葷粺涓嬫墽琛屽懡浠わ細man inotify銆?man inotifywait銆?man inotifywatch鍗沖彲寰楀埌鐩稿簲鐨勫府鍔╀俊鎭紝琛ㄧずinotify瀹夎鎴愬姛銆?br />
12銆佽緭鍏ュ懡浠わ細rsync錛屾煡鐪媟sync鏄惁瀹夎銆?br />
rsync涓鑸槸緋葷粺榛樿瀹夎錛屽鏋滄病鏈夊畨瑁呭氨鍙栧緱杞歡鍖咃紝瀹夎鏂規(guī)硶鍚宨notify銆?/p>
鍚屾鑴氭湰浣跨敤
1銆佸彇寰梥yncapps.sh鑴氭湰
#!/bin/sh
SRC=/root/sys/
SEND=iEPGService.dat
PID_FILE=syncapps.pid
function sync_files
{
cat $SEND | while read DST
do
rsync -avzq --delete --exclude '/.version' --exclude '/.bak' $SRC $DST
done
}
function inotify_func
{
inotifywait -mrq -e modify,delete,create ${SRC} | while read D E F;do
# echo "$D : $E : $F"
sync_files
done
}
function stop
{
pkill inotifywait &>/dev/null && rm -f ${PID_FILE} &> /dev/null
}
case $1 in
stop)
echo -n "Stopping sync service
"
if [ -e ${PID_FILE} ]; then
stop
echo "Stopped"
exit 0
else
echo "pid file not found"
exit 2
fi
;;
start)
echo -n "Starting sync service
"
if [ -f ${PID_FILE} ] && ((`ps awux | grep -v grep | grep -c inotifywait`)); then
echo " already running: pid file found ($PID_FILE) and an inotifywait process is running"
exit 1
elif [ -f ${PID_FILE} ]; then
echo -n "(stale pid file)"
fi
sync_files
inotify_func&
pid="$!"
ps --ppid $pid -o pid,cmd | grep inotifywait | awk '{print $1}' > ${PID_FILE}
echo "Started"
;;
restart)
$0 stop
$0 start
exit 0
;;
status)
echo -n "Getting status for syncer service
"
pid=`cat ${PID_FILE} 2>/dev/null`
if [ -f ${PID_FILE} ] && ((`ps awux | grep -v grep | egrep -c "$pid.*inotifywait"`)); then
echo "running (pid $pid)"
exit 0
elif [ -f ${PID_FILE} ]; then
echo "not runing (pid file found $pid)"
exit 3
elif ((`ps awux | grep -v grep | egrep -c "$pid.*inotifywait"`)); then
echo "not running (inotifywait procs found)"
exit 4
else
echo "not running"
exit 5
fi
;;
*)
echo "Usage error"
echo "Usage: $0 <start|stop|restart|status>"
;;
esac
2銆佸彇寰梚EPGService.dat鑴氭湰銆?br />
root@10.10.80.76:/root/files/
3銆佽緭鍏ュ懡浠わ細chmod +x *.sh錛岀粰鏂囦歡璧嬪彲鎵ц鏉冮檺銆?br />
4銆佽緭鍏ュ懡浠わ細./syncapps.sh start錛屽惎鍔ㄥ悓姝ュ伐鍏楓?br />
鍚姩鍚屾宸ュ叿鐨勮緭鍏ュ懡浠わ細./syncapps.sh start
鍋滄鍚屾宸ュ叿鐨勮緭鍏ュ懡浠わ細./syncapps.sh stop
閲嶅惎鍚屾宸ュ叿鐨勮緭鍏ュ懡浠わ細./syncapps.sh restart
鏌ョ湅鍚屾宸ュ叿鐘舵佺殑杈撳叆鍛戒護錛?/syncapps.sh status
link

]]>