posts - 5,  comments - 6,  trackbacks - 0

Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。下面让我们一起学习如何安装 inotify 和如何构建一个示例用户空间应用程序来响应文件系统事件。

1.1同步工具安装
1、输入命令:su root,切换到超级用户。
2、先查看linux的内核是否支持inotify,支持inotify的内核最小为2.6.13,输入命令:uname –a。如下图所示,内核为2.6.27,支持inotify:
 
注:如果内核低于2.6.13,请升级内核或重新安装内核版本更高的linux系统。
3、建立同步ssh信任关系,输入命令:cd $HOME,进入用户根目录。
输入命令:ssh-keygen -t rsa (会出现几个提示信息,一直按回车即可)。
会在 cd $HOME/.ssh/目录下生成2个文件id_rsa、id_rsa.pub。
输入命令:cp  id_rsa.pub  authorized_keys,将id_rsa.pub拷贝成authorized_keys。
将授权密钥分发到iEPG服务器(192.168.100.101)上,输入命令:
scp  ~/.ssh/authorized_keys root@192.168.100.101:/root/.ssh/
如果有多台下载服务器,每台都须运行一次上面的密钥下发命令。
4、通过如下命令查看系统是否支持inotify:ll /proc/sys/fs/inotify
如果有如下输出,表示系统内核已经支持inotify:
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下。
6、输入命令:tar zvxf inotify-tools-3.13.tar.gz,解压软件包。
7、输入命令:cd inotify-tools-3.13,进入解压后的目录。
8、输入命令:./configure
9、输入命令:make
10、输入命令:make install
11、在系统下执行命令:man inotify、 man inotifywait、 man inotifywatch即可得到相应的帮助信息,表示inotify安装成功。
12、输入命令:rsync,查看rsync是否安装。
rsync一般是系统默认安装,如果没有安装就取得软件包,安装方法同inotify。

同步脚本使用
1、取得syncapps.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、取得iEPGService.dat脚本。
  root@10.10.80.76:/root/files/
3、输入命令:chmod  +x  *.sh,给文件赋可执行权限。
4、输入命令:./syncapps.sh start,启动同步工具。
启动同步工具的输入命令:./syncapps.sh start
停止同步工具的输入命令:./syncapps.sh stop
重启同步工具的输入命令:./syncapps.sh restart
查看同步工具状态的输入命令:./syncapps.sh status
link
posted on 2010-01-10 20:07 浔阳江头夜送客 阅读(1356) 评论(0)  编辑  收藏 所属分类: linux

只有注册用户登录后才能发表评论。


网站导航:
 
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔分类

随笔档案

myeclipse6.5上基于JAX-WS开发Webservice(中文示例)

搜索

  •  

最新评论

阅读排行榜

评论排行榜