inotifywait命令如何监控文件变化?
转载自:https://segmentfault.com/a/1190000038351925
文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)
实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(可用于代码自动发布)。
inotify 是linux内核的一个特性,在内核 2.6.13 以上都可以使用。
如果在shell环境下,可以安装 yum install inotify-tools,安装以后有两个命令可以用inotifywait 和 inotifywatch,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命令如何监控文件变化?的更多相关文章
- JDK 之 NIO 2 WatchService、WatchKey(监控文件变化)
JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html ...
- Python监控文件变化:watchdog
Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...
- mac 监控文件变化并重启php
自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...
- linux 监控文件变化
介绍 有时候我们常需要当文件变化的时候便触发某些脚本操作,比如说有文件更新了就同步文件到远程机器.在实现这个操作上,主要用到两个工具,一个是rsync,一个是inotifywait .inotifyw ...
- Gulp-前端进阶A-3---如何不刷新监控文件变化?
npm install --save-dev gulp-connect npm install --save-dev gulp-livereload npm其他,前面已有 var gulp = req ...
- 使用apache common-io 监控文件变化--转
package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...
- 利用nodejs监控文件变化并使用sftp上传到服务器
很久没写博客了,因为最近在用react+express做一个自己的工具型网站(其实就是夺宝岛抢拍器) 然后因为经常要改动,而且又要放到服务器上进行测试.总是要webpack,然后手动把文件上传上去,不 ...
- 使用apache common-io 监控文件变化
package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...
- inotify监控文件变化
1.安装inotify-tools yum install make gcc gcc-c++ #安装编译工具 inotify-tools下载地址:http://github.com/downloa ...
随机推荐
- C++高级程序员进阶之路
一.自学成为高级程序员推荐看的书: 1.c语言基础 <c primer Plus>.<c和指针>.<C专家编程> 2.C++语言基础 <C++ Primer& ...
- Linux的硬盘挂载
一·前言 我朋友买了一个香港的服务器,可用总容量为60G,实际只有15.4G,剩下的容量需要硬盘挂载.他尝试无果,向我求助.我帮他解决了问题,想回顾一下整理写此随笔. 二·运行环境 Linux系统版本 ...
- [Android systrace系列] systrace的信息从哪里来
-------------------------------------------------------------- 这篇文章的小目标: 1. systrace是怎么抓出来的 2. 这些信息的 ...
- CentOS 6.5 iso系统定制
前言 更改CentOS6.5背景图片.CentOS标题为DntOS,总之就是用ISO安装或者安装后的系统启动时不能有CentOS标志. ISO光盘目录介绍: (1)isolinux 目录存放光盘启动时 ...
- 三万字无坑搭建基于Docker+K8S+GitLab/SVN+Jenkins+Harbor持续集成交付环境
写在前面 最近在 K8S 1.18.2 版本的集群上搭建DevOps环境,期间遇到了各种坑.目前,搭建环境的过程中出现的各种坑均已被填平,特此记录,并分享给大家! 文章和搭建环境所需要的yml文件已收 ...
- moviepy音视频剪辑:headblur函数遇到的TypeError: integer argument expected, got float错误的解决方案
运行环境如下: python版本:3.7 opencv-python版本:4.2.0.34 numpy版本:1.19.0 错误信息: 在调用moviepy1.03版本的headblur函数执行人脸跟踪 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项下的子项列表中增加子项的方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 追加子项 QTreeWidgetItem类型的项构建以后,可以通过addChild(QTreeWid ...
- 【开发工具】 使用阿里的 Teambition 跟踪BUG修复及需求管理
刚入公司几个月,公司新项目一直没有用协作工具进行BUG管理,公司也没有专业的测试工程师. 第一阶段开发到了尾声,工作效率实在太低,随决定使用一个比较现代一点的管理工具. 在众多在线项目协作软件中,我选 ...
- python-字典dict、去除重复set
dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...
- SpringCloud Alibaba学习笔记
目录 目录 目录 导学 为什么学 学习目标 进阶目标 思路 Spring Cloud Alibaba的重要组件 环境搭建 Spring Boot必知必会 Spring Boot特性 编写第一个Spri ...