转载自:https://segmentfault.com/a/1190000038351925

文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)

实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)。

inotify 是linux内核的一个特性,在内核 2.6.13 以上都可以使用。

如果在shell环境下,可以安装 yum install inotify-tools,安装以后有两个命令可以用inotifywaitinotifywatch,inotifywait 是需要使用的命令。

 

 

 监听/usr/local/src目录:

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib /usr/local/src/

参数:

[root@elk1 /usr/local/src]# inotifywait --help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
        -h|--help       Show this help text.
        @<file>         Exclude the specified file from being watched.
        --exclude <pattern>
                        Exclude all events on files matching the
                        extended regular expression <pattern>.
        --excludei <pattern>
                        Like --exclude but case insensitive.
        -m|--monitor    Keep listening for events forever.  Without
                        this option, inotifywait will exit after one
                        event is received.
        -d|--daemon     Same as --monitor, except run in the background
                        logging events to a file specified by --outfile.
                        Implies --syslog.
        -r|--recursive  Watch directories recursively.
        --fromfile <file>
                        Read files to watch from <file> or `-' for stdin.
        -o|--outfile <file>
                        Print events to <file> rather than stdout.
        -s|--syslog     Send errors to syslog rather than stderr.
        -q|--quiet      Print less (only print events).
        -qq             Print nothing (not even events).
        --format <fmt>  Print using a specified printf-like format
                        string; read the man page for more details.
        --timefmt <fmt> strftime-compatible format string for use with
                        %T in --format string.
        -c|--csv        Print events in CSV format.
        -t|--timeout <seconds>
                        When listening for a single event, time out after
                        waiting for an event for <seconds> seconds.
                        If <seconds> is 0, inotifywait will never time out.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are 
                listened for.

Exit status:
        0  -  An event you asked to watch for was received.
        1  -  An event you did not ask to watch for was received
              (usually delete_self or unmount), or some error occurred.
        2  -  The --timeout option was given and no events occurred
              in the specified interval of time.

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
  • -m 持续监听
  • -r 使用递归形式监视目录
  • -q 减少冗余信息,只打印出需要的信息
  • -e 指定要监视的事件,多个时间使用逗号隔开
  • --timefmt 时间格式
  • --format 监听到的文件变化的信息
  • --timefmt  说明:ymd分别表示年月日,H表示小时,M表示分钟

--format说明:

参数 说明
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由--timefmt定义的时间格式

 

执行上面的命令之后,在监听的目录下创建一个文件,得到如下结果:

 

 

这个脚本的功能是循环监听文件或目录的增删改事件,当事件发生执行设置的脚本文件。

#!/bin/bash
# 监视的文件和目录
filename=$1
# 监视发现有增删改时执行的脚本
script=$2
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib $filename | while read event
do
    case $event in MODIFY|CREATE|DELETE) bash $scripts ;;
    esac
done

shell脚本后台执行

使用nohup指令

其中,test.sh是所执行的脚本,out.txt为输出信息的地方。

nohup sh test.sh>out.txt &
posted on 2020-12-03 16:32  jiayou111  阅读(589)  评论(0编辑  收藏  举报