[Go] 跨平台文件系统监控工具 fsnotify 应用举例
项目地址:https://github.com/fsnotify/fsnotify
fsnotify 能监控指定文件夹内 文件的修改情况,如 文件的 增加、删除、修改、重命名等操作。
官方给出了以下注意事项:
When a file is moved to another directory is it still being watched?
No (it shouldn't be, unless you are watching where it was moved to).
When I watch a directory, are all subdirectories watched as well?
No, you must add watches for any directory you want to watch (a recursive watcher is on the roadmap #18).
Do I have to watch the Error and Event channels in a separate goroutine?
As of now, yes. Looking into making this single-thread friendly (see howeyc #7)
Why am I receiving multiple events for the same file on OS X?
Spotlight indexing on OS X can result in multiple events (see howeyc #62). A temporary workaround is to add your folder(s) to the Spotlight Privacy settings until we have a native FSEvents implementation (see #11).
How many files can be watched at once?
There are OS-specific limits as to how many watches can be created:
- Linux: /proc/sys/fs/inotify/max_user_watches contains the limit, reaching this limit results in a "no space left on device" error.
- BSD / OSX: sysctl variables "kern.maxfiles" and "kern.maxfilesperproc", reaching these limits results in a "too many open files" error.
最为注意的一点是:
文件夹中的子文件夹,还需自己去添加监控,fsnotify 本身不提供递归循环监控功能!
应用举例
package main import (
"github.com/fsnotify/fsnotify"
"log"
"runtime"
) func main() {
// 监控路径列表
paths := []string{
"/Users/jianbao/GoglandProjects/fiisoo/src/test/ch0",
"/Users/jianbao/GoglandProjects/fiisoo/src/test/ch1",
} watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Failed to create watcher: %s", err)
}
defer watcher.Close() exit := make(chan bool) go func() {
for {
select {
case e := <-watcher.Events:
log.Println("修改文件:" + e.Name)
log.Println("修改类型:" + e.Op.String())
case err := <-watcher.Errors:
log.Printf("Watcher error: %s\n", err.Error()) // No need to exit here
}
}
}() log.Println("Initializing watcher...")
for _, path := range paths {
log.Printf("Watching: %s\n", path)
err = watcher.Add(path)
if err != nil {
log.Fatalf("Failed to watch directory: %s", err)
}
} <-exit // 用来 阻塞应用不退出,只能通过“杀死进程”的方式退出,如 按住 Ctrl + C 快捷键强制推出
runtime.Goexit()
}
[Go] 跨平台文件系统监控工具 fsnotify 应用举例的更多相关文章
- C++轻量级跨平台文件系统API
http://en.cppreference.com/w/cpp/experimental/fs https://www.starmessagesoftware.com/cpcclibrary htt ...
- 学习笔记:CentOS7学习之十四:linux文件系统
目录 1. 机械硬盘结构 1.1 机械硬盘结构 1.2 簇和block 2.文件系统结构 2.1 文件名 2.2 inode的内容 2.3 inode的大小 2.4 目录文件 2.5 block块大小 ...
- 使用Python监控Linux系统
一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...
- 优秀的 Go 存储开源项目和库
可以看到,今年谷歌家的 Go 编程语言流行度有着惊人的上升趋势,其发展也是越来越好,因此本文整理了一些优秀的 Go 存储相关开源项目和库,一起分享,一起学习. 存储服务器(Storage Server ...
- 第6章:使用Python监控Linux系统
1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...
- Linux资源监控命令/工具(调试)
1.直接将指令丢到背景中执行:& [root@linux ~]# tar -zpcvf /tmp/etc.tar.gz /etc > /tmp/log.txt 2>&1 & ...
- ioc容器
对于容器而言需要满足两个方面: 1.全局唯一 2.无论何地都可以进行对容器的访问 对于Spring而言,BeanFactory则就是这样的容器,只不过它过于底层.在我们的日常开发中还是使用Applic ...
- Linux常用命令及使用技巧
本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...
- Linux命令——df/du/time
一.df(disk free) df命令可以用来检查 linux服务器的文件系统的磁盘空间占用情况,可以知道硬盘被占用了多少空间,目前还剩下多少空间等信息. 1)命令格式 df [参数] 文件名 2) ...
随机推荐
- 【黑客免杀攻防】读书笔记10 - switch-case分支
0x1 switch-case分支 switch-case其实就是if-else语句的另一种体现形式.但大于3之后的switchc-case.编译器会对代码进行优化. 1.1 简单switch-cas ...
- 基于FPGA(DDS)的正弦波发生器
记录背景:昨晚快下班时,与同事rk聊起怎么用FPGA实现正弦波的输出.我第一反应是利用高频的PWM波去滤波,但感觉这样的波形精度肯定很差:后来想起之前由看过怎么用FPGA产生正弦波的技术,但怎么都想不 ...
- SSH2框架搭建 和 配置文件详解
-----------补充说明----------- 文章中所列出的struts2的2.2jar包已经不是最新的了,这个版本有严重漏洞, 现在最新版本为2.3.15,所以.你懂的http://stru ...
- poj1221
dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...
- JMeter出现“the target server failed to respond“的解决办法
今天用jmeter压测执行过程中遇到一个报错如下: 解决方案如下: 1. 修改执行计划中,HTTP请求的Implementation为HttpClient4. 2. 保存执行计划 3. 修改JMete ...
- javascript-序列化,时间,eval,转义
一:序列化 JSON.stringify(li) 将对象转字符串 JSON.parse(str1) 将字符串转对象 li=[11,22,33] [11, 22, 33] li [11, 22, 33] ...
- Coursera台大机器学习技法课程笔记14-Radial Basis Function Network
将Radial Basis Function与Network相结合.实际上衡量两个点的相似性:距离越近,值越大. 将神经元换为与距离有关的函数,就是RBF Network: 可以用kernel和RBF ...
- DedeCMS常见问题和技巧
1: dedecms 访问空白(织梦如何显示详细错误) 我们在使用织梦的时候,有的时候会遇到访问空白的情况,尤其是再刚刚搬家之后,织梦会出现访问空白或者返给您一个500的友好界面错误,遇到这种情况该怎 ...
- CentOS 7.x 安装 Docker
安装之前确保之前没有安装过docker为此首先删除存在的docker程序 sudo yum remove docker \ docker-common \ docker-selinux \ docke ...
- ngResource和REST介绍
ngResource和REST介绍 一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding ...