转载自: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 &

inotifywait命令如何监控文件变化?的更多相关文章

  1. JDK 之 NIO 2 WatchService、WatchKey(监控文件变化)

    JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html ...

  2. Python监控文件变化:watchdog

    Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...

  3. mac 监控文件变化并重启php

    自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...

  4. linux 监控文件变化

    介绍 有时候我们常需要当文件变化的时候便触发某些脚本操作,比如说有文件更新了就同步文件到远程机器.在实现这个操作上,主要用到两个工具,一个是rsync,一个是inotifywait .inotifyw ...

  5. Gulp-前端进阶A-3---如何不刷新监控文件变化?

    npm install --save-dev gulp-connect npm install --save-dev gulp-livereload npm其他,前面已有 var gulp = req ...

  6. 使用apache common-io 监控文件变化--转

    package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...

  7. 利用nodejs监控文件变化并使用sftp上传到服务器

    很久没写博客了,因为最近在用react+express做一个自己的工具型网站(其实就是夺宝岛抢拍器) 然后因为经常要改动,而且又要放到服务器上进行测试.总是要webpack,然后手动把文件上传上去,不 ...

  8. 使用apache common-io 监控文件变化

    package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...

  9. inotify监控文件变化

    1.安装inotify-tools yum install make  gcc gcc-c++  #安装编译工具 inotify-tools下载地址:http://github.com/downloa ...

随机推荐

  1. iOS沙盒文件目录介绍

    1.APP沙盒目录结构简介 首先奉上苹果官方文档: https://developer.apple.com/library/archive/documentation/FileManagement/C ...

  2. C++基础入门知识:C++命名空间(名字空间)详解

    一个中大型软件往往由多名程序员共同开发,会使用大量的变量和函数,不可避免地会出现变量或函数的命名冲突.当所有人的代码都测试通过,没有问题时,将它们结合到一起就有可能会出现命名冲突. 例如小李和小韩都参 ...

  3. dubbo与zk

    一.总体流程: 1.服务提供者启动时,会向注册中心写入自己的元数据信息,同时会订阅配置元数据信息: 2.消费者启动时,也会向注册中心写入自己的元数据信息,并订阅服务提供者.路由和配置元数据信息: 3. ...

  4. rest-framework 解析器

    一 解析器的作用: 根据请求头 content-type 选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格 ...

  5. ES6简单理解基本使用

    let const 原来的var声明标识符:可以重复声明,编译不报错. let,const声明标识符:不能重复声明,再声明编译报错. var声明的标识符作用域是当前函数,let和const是当前{块} ...

  6. 因为一个Docker问题,我顺手整理从安装到常用命令操作手册

    今天,自己写了一部分业务代码,是常规代码的另外一种方式,不能在公司的服务器上测试,就自己在PC端搭建了一套和公司集群一样的模板,因为公司的业务模块的测试有单独的服务器(这一块还是我很稀罕的),但是,第 ...

  7. 小程序view的显示与隐藏

    需要在全局数据块中,设定一个控制键. data: { ......//省略其他代码 showView: true }, 然后是在wxml中,view的class中设置2个class,并用三目表达式来进 ...

  8. Scrum冲刺_Day04

    一.团队展示: 1.项目:light_note备忘录 2.队名:删库跑路队 3.团队成员 队员(不分先后) 项目角色 黄敦鸿 后端工程师.测试 黄华 后端工程师.测试 黄骏鹏 后端工程师.测试 黄源钦 ...

  9. mvvm和mvc区别?

    mvc和mvvm其实区别并不大.都是一种设计思想.主要就是mvc中Controller演变成mvvm中的viewModel. mvvm主要解决了mvc中大量的DOM 操作使页面渲染性能降低,加载速度变 ...

  10. 【题解】P1852 跳跳棋

    link 题意 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.棋盘上有3颗棋子,分别在 \(a,b,c\) 这三个位置.我们要通过最少的跳动把他们的位置移动成 \(x,y, ...