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

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

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

监听/usr/local/src 目录:

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

参数:

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

--format说明:

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

22/03/18 17:22 /usr/local/src/ 1.txt CREATE 
22/03/18 17:22 /usr/local/src/ 1.txt ATTRIB

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

#!/bin/sh 
 # 监视的文件或目录 
 filename=$1 
 # 监视发现有增、删、改时执行的脚本 
 script=$2 
 
 inotifywait -mrq --format '%e' --event create,delete,modify  $filename | while read event 
 do 
     case $event in MODIFY|CREATE|DELETE) bash $script ;; 
     esac 
 done

shell脚本后台执行

nohup

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

nohup sh test.sh>out.txt &

原文来自:https://os.51cto.com/art/202012/633517.htm

本文地址:https://www.linuxprobe.com/shell-files.html编辑:roc_guo,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/