posts - 5,  comments - 6,  trackbacks - 0
            2010年1月10日

          Inotify 是文件系統(tǒng)事件監(jiān)控機(jī)制,計(jì)劃包含在即將發(fā)布的 Linux 內(nèi)核中作為 dnotify 的有效替代。dnotify 是較早內(nèi)核支持的文件監(jiān)控機(jī)制。Inotify一種強(qiáng)大的、細(xì)粒度的、異步的機(jī)制,它滿足各種各樣的文件監(jiān)控需要,不僅限于安全和性能。下面讓我們一起學(xué)習(xí)如何安裝 inotify 和如何構(gòu)建一個(gè)示例用戶空間應(yīng)用程序來響應(yīng)文件系統(tǒng)事件。

          1.1同步工具安裝
          1、輸入命令:su root,切換到超級用戶。
          2、先查看linux的內(nèi)核是否支持inotify,支持inotify的內(nèi)核最小為2.6.13,輸入命令:uname –a。如下圖所示,內(nèi)核為2.6.27,支持inotify:
           
          注:如果內(nèi)核低于2.6.13,請升級內(nèi)核或重新安裝內(nèi)核版本更高的linux系統(tǒng)。
          3、建立同步ssh信任關(guān)系,輸入命令:cd $HOME,進(jìn)入用戶根目錄。
          輸入命令:ssh-keygen -t rsa (會出現(xiàn)幾個(gè)提示信息,一直按回車即可)。
          會在 cd $HOME/.ssh/目錄下生成2個(gè)文件id_rsa、id_rsa.pub。
          輸入命令:cp  id_rsa.pub  authorized_keys,將id_rsa.pub拷貝成authorized_keys。
          將授權(quán)密鑰分發(fā)到iEPG服務(wù)器(192.168.100.101)上,輸入命令:
          scp  ~/.ssh/authorized_keys root@192.168.100.101:/root/.ssh/
          如果有多臺下載服務(wù)器,每臺都須運(yùn)行一次上面的密鑰下發(fā)命令。
          4、通過如下命令查看系統(tǒng)是否支持inotify:ll /proc/sys/fs/inotify
          如果有如下輸出,表示系統(tǒng)內(nèi)核已經(jīng)支持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,進(jìn)入解壓后的目錄。
          8、輸入命令:./configure
          9、輸入命令:make
          10、輸入命令:make install
          11、在系統(tǒng)下執(zhí)行命令:man inotify、 man inotifywait、 man inotifywatch即可得到相應(yīng)的幫助信息,表示inotify安裝成功。
          12、輸入命令:rsync,查看rsync是否安裝。
          rsync一般是系統(tǒng)默認(rèn)安裝,如果沒有安裝就取得軟件包,安裝方法同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,給文件賦可執(zhí)行權(quán)限。
          4、輸入命令:./syncapps.sh start,啟動同步工具。
          啟動同步工具的輸入命令:./syncapps.sh start
          停止同步工具的輸入命令:./syncapps.sh stop
          重啟同步工具的輸入命令:./syncapps.sh restart
          查看同步工具狀態(tài)的輸入命令:./syncapps.sh status
          link
          posted @ 2010-01-10 20:07 潯陽江頭夜送客 閱讀(1391) | 評論 (0)編輯 收藏

          首先閱讀此文之前,最好閱讀
          http://hi.baidu.com/maml897/blog/item/324bf86369961ed4e6113a5c.html

          http://hi.baidu.com/maml897/blog/item/fa5f0a7e1edef00129388ae2.html

          其次還要知道一點(diǎn)常識,就是我們在記事本等一些文本工具中 寫的都是字符,沒有誰會去寫字節(jié)(可以寫字節(jié),但是要用具特殊的編輯器),但是其實(shí),我們的寫的是字符,但磁盤上真實(shí)存儲的是字節(jié)。

          這里就出現(xiàn)了轉(zhuǎn)換的問題,當(dāng)然,這些問題記事本本身會幫助我們解決。我們打開一個(gè)記事本,然后文件--另存為,你會發(fā)現(xiàn)有幾種存儲格式供您選擇,
          ANSI格式:就是ascii的格式
          Unicode格式:采用國際通用的編碼存儲
          Unicode big endian格式:這個(gè)和unicode有點(diǎn)區(qū)別,但我也不明太具體的不同
          UTF-8:采用utf-8存儲,看過上面的兩篇文章,你會十分的了解這里介紹的編碼。Utf-8,是unicode的一種實(shí)現(xiàn)方式。

          例如我們在記事本里面輸入“連通”兩個(gè)字。

          1.我們另存記事本的時(shí)候,采用unicode存儲,那么雖然我們看到的字符還是“連通”,但是其實(shí)存儲在磁盤上的字節(jié) 確實(shí)
          8FDE(連) 901A (通),這個(gè)是規(guī)定的,unicode是國際上規(guī)定的,給世界上的每個(gè)字符分配的唯一編碼。獲取某個(gè)字符的unicode的方法,可以去網(wǎng)上查找,最簡單的方法,就是打開word文檔,輸入字符,把光標(biāo)移動到字符后面,按alt+x,word會自動把字符轉(zhuǎn)換成unicode編碼,這里呢我們也可以看到,用unicode存儲漢字啊,每個(gè)漢字占用兩個(gè)字節(jié)。

          2.我們另存記事本的時(shí)候,采用utf-8存儲,雖然我們看到的字符還是“連通”,但是其實(shí)存儲在磁盤上的字節(jié) 確實(shí)已經(jīng)變化了,這時(shí)候存儲的是
          E8 BF 9E (連)E9 80 9A(通)。這就是utf-8的存儲的編碼,至于utf-8為什么這樣存儲,你可以閱讀上面的兩篇文章來了解,可以看到,utf-8使用3個(gè)字節(jié)存儲一個(gè)漢字。

          另外我們還要知道的就是:電腦怎么區(qū)分一個(gè)記事本是用什么存儲的呢?
          換句話說,為什么我用unicode存儲的8FDE(連) 901A (通),電腦就知道這是unicode編碼,從而使用unicode解碼,還原為“連通”呢?電腦又怎么知道E8 BF 9E (連)E9 80 9A(通)這是按照utf-8的存儲方式存儲的呢?

          這里有一點(diǎn)標(biāo)記,就是在存儲字節(jié)的時(shí)候,記事本首先在最前面 標(biāo)明,這個(gè)記事本下面的存儲格式 是utf-8,還是unicode。

          例如,

          1.unicode存儲“連通”。磁盤字節(jié)真實(shí)存儲的其實(shí)是:

          FF FE 8FDE 901A

          前兩個(gè)FF FE是標(biāo)記,告訴電腦,這個(gè)文檔的存儲方式是unicode

          2.utf-8存儲“連通”。磁盤字節(jié)真實(shí)存儲的其實(shí)是:

          EF BB BF E8 BF 9E E9 80 9A

          前三個(gè)EF BB BF 告訴電腦 這個(gè)文檔是utf-8存儲的

          根據(jù)不同編碼的特點(diǎn)和標(biāo)志,對一個(gè)文本文件判斷編碼方法如下
          1  .  UTF7  所有字節(jié)的內(nèi)容不會大于127,也就是不大于&HFF
          2  .  UTF8  起始三個(gè)字節(jié)為"0xEF 0xBB 0xBF"
          3  .  UTF-16BE 起始三個(gè)字節(jié)為"0xFE  0xFF"
          4  .  UTF-16LE 起始三個(gè)字節(jié)為"0xFF  0xFE"


          import java.io.BufferedInputStream;
          import java.io.File;
          import java.io.FileInputStream;

          public class FileEncodeReferee
          {
              
          private File file;
              
              
          public FileEncodeReferee(File file)
              
          {
                  
          this.file = file;
              }

              
              
          public FileEncodeReferee(String path)
              
          {
                  file 
          = new File(path);
              }

              
              
          public String getCharset()
              
          {
                  File file 
          = this.file;
                  
                  String charset 
          = "GBK";
                  
          byte[] first3Bytes = new byte[3];
                  BufferedInputStream bis 
          = null;
                  
          try
                  
          {
                      
          //boolean checked = false;
                      bis = new BufferedInputStream(new FileInputStream(file));
                      bis.mark(
          0);
                      
          int read = bis.read(first3Bytes, 03);
                      
          if (read == -1)
                      
          {
                          
          return charset;
                      }

                      
          if (first3Bytes[0== (byte0xFF && first3Bytes[1== (byte0xFE)
                      
          {
                          charset 
          = "UTF-16LE";
                          
          //checked = true;
                      }

                      
          else if (first3Bytes[0== (byte0xFE
                              
          && first3Bytes[1== (byte0xFF)
                      
          {
                          charset 
          = "UTF-16BE";
                          
          //checked = true;
                      }

                      
          else if (first3Bytes[0== (byte0xEF
                              
          && first3Bytes[1== (byte0xBB
                              
          && first3Bytes[2== (byte0xBF)
                      
          {
                          charset 
          = "UTF-8";
                          
          //checked = true;
                      }

                      
          /** */
                      
          /*******************************************************************
                      * bis.reset(); if (!checked) { int loc = 0; while ((read =
                      * bis.read()) != -1) { loc++; if (read >= 0xF0) { break; } if (0x80 <=
                      * read && read <= 0xBF) // 單獨(dú)出現(xiàn)BF以下的,也算是GBK { break; } if (0xC0 <=
                      * read && read <= 0xDF) { read = bis.read(); if (0x80 <= read &&
                      * read <= 0xBF)// 雙字節(jié) (0xC0 - 0xDF) { // (0x80 - 0xBF),也可能在GB編碼內(nèi)
                      * continue; } else { break; } } else if (0xE0 <= read && read <=
                      * 0xEF) { // 也有可能出錯(cuò),但是幾率較小 read = bis.read(); if (0x80 <= read &&
                      * read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <=
                      * 0xBF) { charset = "UTF-8"; break; } else { break; } } else {
                      * break; } } } System.out.println(loc + " " +
                      * Integer.toHexString(read)); }
                      *****************************************************************
          */

                  }

                  
          catch (Exception e)
                  
          {
                      e.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          if (bis != null)
                      
          {
                          
          try
                          
          {
                              bis.close();
                          }

                          
          catch (Exception ex)
                          
          {
                              ex.printStackTrace();
                          }

                      }

                  }

                  
          return charset;
              }

              
              
          public static void main(String[] args)
              
          {
                  FileEncodeReferee fer 
          = new FileEncodeReferee("F://鎖表1.sql");
                  System.out.println(fer.getCharset());
              }

          }

          posted @ 2010-01-10 19:56 潯陽江頭夜送客 閱讀(406) | 評論 (0)編輯 收藏
          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

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

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 环江| 江华| 健康| 亳州市| 屯昌县| 温宿县| 夹江县| 平昌县| 徐州市| 台山市| 金坛市| 清徐县| 大方县| 怀安县| 玉环县| 丰镇市| 大城县| 汾西县| 宜都市| 平谷区| 韶关市| 河西区| 遵义市| 青阳县| 甘泉县| 巴林左旗| 尉犁县| 湖北省| 家居| 湄潭县| 马公市| 普陀区| 玛纳斯县| 肇州县| 太白县| 五华县| 东至县| 昆明市| 洛扎县| 曲水县| 巴里|