文件监控能够配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)html
实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)。linux
inotify 是linux内核的一个特性,在内核 2.6.13 以上均可以使用。shell
若是在shell环境下,能够安装 yum install inotify-tools,安装之后有两个命令能够用inotifywait 和 inotifywatch,inotifywait 是须要使用的命令。bash
监听/usr/local/src 目录:app
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib /usr/local/src
参数:spa
--format说明:执行上面的命令以后,在监听的目录下建立一个1.txt文件,获得以下结果:3d
22/03/18 17:22 /usr/local/src/ 1.txt CREATE 22/03/18 17:22 /usr/local/src/ 1.txt ATTRIB
这个脚本的功能是循环监听文件或目录的增删改事件,当事件发生执行设置的脚本文件。code
#!/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
nohuporm
使用nohup,其中test.sh为所执行的脚本,out.txt为输出信息的地方。htm
nohup sh test.sh>out.txt &
来源:https://www.cnblogs.com/youxi...