[Go] 跨平台文件系统监控工具 fsnotify 应用举例
2017-05-10 15:19
471 查看
项目地址: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 本身不提供递归循环监控功能!
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语言 系统监控常用工具
- [置顶] web文件管理系统和日志实时监控工具
- S3C6410嵌入式应用平台构建(六)——linux-3.14.4移植到OK6410-(Yaffs2文件系统移植)
- Linux grep工具应用实例 精简配置文件 从系统管理命令输出中查找
- 一共81个,开源大数据处理工具汇总:查询引擎、流式计算、迭代计算、离线计算、键值存储、表格存储、文件存储、资源管理、日志收集系统、消息系统、分布式服务、集群管理、基础设施、搜索引擎、数据挖掘=监控
- linux网络监控工具---系统自带的工具的使用示例(可以监控经过本局域网的任何数据,并可以打印到屏幕或者日志文件)
- 实时应用系统监控平台
- Go -- 如何使用gcore工具获取一个core文件而不重启应用?
- 最近刚为公司完成的一款监视的小工具软件!用DELPHI 7 写的,可以从后台监控系统﹑键盘﹑鼠标﹑屏幕以及文件与目录!有源码!
- 【监控】数据平台运营实战之如何打造应用级别的监控系统
- 监控系统解围企业应用集成平台困境
- 智能文件服务系统应用场景举例 推荐
- 一共81个,开源大数据处理工具汇总:查询引擎、流式计算、迭代计算、离线计算、键值存储、表格存储、文件存储、资源管理、日志收集系统、消息系统、分布式服务、集群管理、基础设施、搜索引擎、数据挖掘=监控
- OPC技术在监控系统中的应用
- 实战揭秘:开发.Net平台应用系统框架 作者:孙亚民 本文选自:赛迪网
- 在从Windows NT 4.0升级至Windows Server 2003的计算机上应用注册表和文件系统ACL
- 开发.Net平台应用系统框架--实践
- 实战揭秘:开发.Net平台应用系统框架 作者:孙亚民 本文选自:赛迪网
- 基于 J2EE 的企业应用系统 - 工具一览表
- 嵌入式系统开发工具及RTOS平台