[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) ...
随机推荐
- 利用iis虚拟目录实现文件服务器功能
要求说明: 通过网站上传文件保存到统一的文件服务器上. 服务器说明: 1.文件服务器以下称为FilesServer,IP地址为:192.168.1.213 2.Web服务器为以下称为WebServer ...
- javascript的this分别代表什么
鉴于大家对this到底代表的是什么有疑问,现在将我个人理解的this的情况整理如下.有错误请指正. 第一种情况: 如果是一个全局的function,则this相当于window对象. 这个打印出来的 ...
- 前端学PHP之正则表达式函数
前面的话 正则表达式不能独立使用,它只是一种用来定义字符串的规则模式,必须在相应的正则表达式函数中应用,才能实现对字符串的匹配.查找.替换及分割等操作.前面介绍了正则表达式的基础语法,本文将详细介绍正 ...
- 【CTF WEB】XSS-https://alf.nu/alert1
XSS练习平台 https://alf.nu/alert1 Warmup 1");alert(1)// Adobe 1");alert(1)// JSON </script& ...
- linux sftp安装【转】
工具:虚拟机:VMware Workstation Pro.操作系统:CentOS-6.4-x86_64-minimal.终端模拟器:Xshell 5 .ftp:filezilla 一.让虚拟机联网 ...
- 从requests源码分析中学习python(一)
v2ex同步更新:https://www.v2ex.com/t/500081 微信公众号:python学习开发 分析源码,看大神的代码是一种学习的好方法,让我从中学到很多以前不知道的知识,这次打算从大 ...
- ORACLE数据库数据文件转移方法(不同于move方法)
1) 手动拷贝要转移的数据数据文件'd:\OracleData\GWTABLE42.DBF'到新的位置'E:\OracleData\GWTABLE42.DBF'. 2) 把数据文件所属的表空间Offl ...
- thymeleaf:访问静态方法
<p class="left tel" th:if="${#strings.startsWith(T(net.common.util.tool.common.Req ...
- C# wpf 阻止*和|的输入
private void texBox_KeyDown(object sender, KeyEventArgs e) { if (Keyboard.Modifiers == ModifierKeys. ...
- .NetCore 结合微服务项目设计总结下实践心得
以下内容全是在项目中的体验,个人理解心得 起源 2017年7月开始接触.NetCore,当时还是因为Idr4的原因,之前的项目都是用的Idr3做,后面接触到Idr4后,决定以后所有项目都使用.NetC ...