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

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 @ 2010-01-10 20:07 浔阳江头夜送客 阅读(1356) | 评论 (0)编辑 收藏

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

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

其次还要知道一点常识,就是我们在记事本等一些文本工具中 写的都是字符,没有谁会去写字节(可以写字节,但是要用具特殊的编辑器),但是其实,我们的写的是字符,但磁盘上真实存储的是字节。

这里就出现了转换的问题,当然,这些问题记事本本身会帮助我们解决。我们打开一个记事本,然后文件--另存为,你会发现有几种存储格式供您选择,
ANSI格式:就是ascii的格式
Unicode格式:采用国际通用的编码存储
Unicode big endian格式:这个和unicode有点区别,但我也不明太具体的不同
UTF-8:采用utf-8存储,看过上面的两篇文章,你会十分的了解这里介绍的编码。Utf-8,是unicode的一种实现方式。

例如我们在记事本里面输入“连通”两个字。

1.我们另存记事本的时候,采用unicode存储,那么虽然我们看到的字符还是“连通”,但是其实存储在磁盘上的字节 确实
8FDE(连) 901A (通),这个是规定的,unicode是国际上规定的,给世界上的每个字符分配的唯一编码。获取某个字符的unicode的方法,可以去网上查找,最简单的方法,就是打开word文档,输入字符,把光标移动到字符后面,按alt+x,word会自动把字符转换成unicode编码,这里呢我们也可以看到,用unicode存储汉字啊,每个汉字占用两个字节。

2.我们另存记事本的时候,采用utf-8存储,虽然我们看到的字符还是“连通”,但是其实存储在磁盘上的字节 确实已经变化了,这时候存储的是
E8 BF 9E (连)E9 80 9A(通)。这就是utf-8的存储的编码,至于utf-8为什么这样存储,你可以阅读上面的两篇文章来了解,可以看到,utf-8使用3个字节存储一个汉字。

另外我们还要知道的就是:电脑怎么区分一个记事本是用什么存储的呢?
换句话说,为什么我用unicode存储的8FDE(连) 901A (通),电脑就知道这是unicode编码,从而使用unicode解码,还原为“连通”呢?电脑又怎么知道E8 BF 9E (连)E9 80 9A(通)这是按照utf-8的存储方式存储的呢?

这里有一点标记,就是在存储字节的时候,记事本首先在最前面 标明,这个记事本下面的存储格式 是utf-8,还是unicode。

例如,

1.unicode存储“连通”。磁盘字节真实存储的其实是:

FF FE 8FDE 901A

前两个FF FE是标记,告诉电脑,这个文档的存储方式是unicode

2.utf-8存储“连通”。磁盘字节真实存储的其实是:

EF BB BF E8 BF 9E E9 80 9A

前三个EF BB BF 告诉电脑 这个文档是utf-8存储的

根据不同编码的特点和标志,对一个文本文件判断编码方法如下
1  .  UTF7  所有字节的内容不会大于127,也就是不大于&HFF
2  .  UTF8  起始三个字节为"0xEF 0xBB 0xBF"
3  .  UTF-16BE 起始三个字节为"0xFE  0xFF"
4  .  UTF-16LE 起始三个字节为"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) // 单独出现BF以下的,也算是GBK { break; } if (0xC0 <=
            * read && read <= 0xDF) { read = bis.read(); if (0x80 <= read &&
            * read <= 0xBF)// 双字节 (0xC0 - 0xDF) { // (0x80 - 0xBF),也可能在GB编码内
            * continue; } else { break; } } else if (0xE0 <= read && read <=
            * 0xEF) { // 也有可能出错,但是几率较小 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 浔阳江头夜送客 阅读(389) | 评论 (0)编辑 收藏
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔分类

随笔档案

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

搜索

  •  

最新评论

阅读排行榜

评论排行榜