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 潯陽江頭夜送客 閱讀(1384) 評論(0)  編輯  收藏 所屬分類: linux

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

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

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富宁县| 揭西县| 盘锦市| 德江县| 万全县| 阿巴嘎旗| 潜江市| 乳山市| 栾城县| 文水县| 稻城县| 西华县| 沧州市| 八宿县| 宿州市| 麻阳| 于都县| 江华| 怀宁县| 子长县| 曲靖市| 唐山市| 泰和县| 大埔县| 富源县| 革吉县| 栾城县| 兴安盟| 泰安市| 仁化县| 东安县| 云龙县| 日喀则市| 吴旗县| 兰坪| 林芝县| 德昌县| 泌阳县| 梅河口市| 长治县| 石河子市|