12 一 2011 No Commentslinux
uname -a #查看linux系統版本號,inotify須要linux內核版本大約2.6.13less
cd /proc/sys/fs/
ls #能夠查看到在目錄下有inotify的目錄
cd inotify
ls #可看到有如下三個文件,說明inotify已經成功安裝
max_queued_events max_user_instances max_user_watchesssh
#安裝inotify-tools-3.14.tar.gz事件捕捉工具
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make && make installide
inotifywatch -h #查看是否有這個命令。若有,安裝已成功wordpress
#如下是inotify所能監控到的目錄或文件的操做
Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted
運行 inotify 設置監控目錄
/usr/local/bin/inotifywait -mrq –timefmt ‘%Y%m%d%H%M’ –format ‘%T %w%f %e’ /home/test/ceshi
–timefmt 時間的格式
–format 輸出的格式
-m 保持一直監聽
-r 是遞歸查看目錄
-q 是打印出事件
-e 監聽的事件工具
官方已個利用inotify 觸發RSYNC同步的例子
#!/bin/sh
# get the current path
CURPATH=`pwd`
inotifywait -mr –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w %f’ \
-e close_write /tmp/test | while read date time dir file; dopost
FILECHANGE=${dir}${file}
# convert absolute path to relative
FILECHANGEREL=`echo 「$FILECHANGE」 | sed ‘s_’$CURPATH’/__’`spa
rsync –progress –relative -vrae ‘ssh -p 22′ $FILECHANGEREL usernam@example.com:/backup/root/dir && \
echo 「At ${time} on ${date}, file $FILECHANGE was backed up via rsync」
done.net
一個的rsync觸發同步例子
#!/bin/sh
src=/data/www/wwwroot/
des=/data/www/wwwroot
ip=192.168.1.101
/usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f’ \ -e modify,delete,create,attrib \ ${src} \ | while read file do rsync -avz –delete –progress ${src} root@${ip}:${des} && #echo 「${src} was rsynced」 #echo 「—————————————————–」 done