项目地址: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 应用举例的更多相关文章

  1. C++轻量级跨平台文件系统API

    http://en.cppreference.com/w/cpp/experimental/fs https://www.starmessagesoftware.com/cpcclibrary htt ...

  2. 学习笔记:CentOS7学习之十四:linux文件系统

    目录 1. 机械硬盘结构 1.1 机械硬盘结构 1.2 簇和block 2.文件系统结构 2.1 文件名 2.2 inode的内容 2.3 inode的大小 2.4 目录文件 2.5 block块大小 ...

  3. 使用Python监控Linux系统

    一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...

  4. 优秀的 Go 存储开源项目和库

    可以看到,今年谷歌家的 Go 编程语言流行度有着惊人的上升趋势,其发展也是越来越好,因此本文整理了一些优秀的 Go 存储相关开源项目和库,一起分享,一起学习. 存储服务器(Storage Server ...

  5. 第6章:使用Python监控Linux系统

    1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...

  6. Linux资源监控命令/工具(调试)

    1.直接将指令丢到背景中执行:& [root@linux ~]# tar -zpcvf /tmp/etc.tar.gz /etc > /tmp/log.txt 2>&1 & ...

  7. ioc容器

    对于容器而言需要满足两个方面: 1.全局唯一 2.无论何地都可以进行对容器的访问 对于Spring而言,BeanFactory则就是这样的容器,只不过它过于底层.在我们的日常开发中还是使用Applic ...

  8. Linux常用命令及使用技巧

    本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...

  9. Linux命令——df/du/time

    一.df(disk free) df命令可以用来检查 linux服务器的文件系统的磁盘空间占用情况,可以知道硬盘被占用了多少空间,目前还剩下多少空间等信息. 1)命令格式 df [参数] 文件名 2) ...

随机推荐

  1. 关于内核中spinlock的一些个人理解 【转】

    由于2.6内核可以抢占,应该在驱动程序中使用 preempt_disable() 和 preempt_enable(),从而保护代码段不被抢占(禁止 IRQ 同时也就隐式地禁止了抢占).preempt ...

  2. 清理电脑文件夹中的Thumbs.db文件

    @echo off del f:Thumbs.db /f/s/q/a exit 对应修改磁盘号, 保存批处理文件执行即可

  3. private,protected,public和default的区别

    private,protected,public和default的区别 private,protected,public和default作为Java中的访问修饰符,他们的最大区别就在于访问权限不同: ...

  4. Redis持久化存储(RDB和AOF)

    参考了: https://blog.csdn.net/canot/article/details/52886923  和 https://www.cnblogs.com/zhangchao-letv/ ...

  5. TinyHttpd代码解析

    十一假期,闲来无事.看了几个C语言开源代码.http://www.cnblogs.com/TinyHttpd 这里本来想解析一下TinyHttpd的代码,但是在网上一搜,发现前辈们已经做的很好了.这里 ...

  6. (转)Python函数式编程——map()、reduce()

    转自:http://www.jianshu.com/p/7fe3408e6048 1.map(func,seq1[,seq2...]) Python 函数式编程中的map()函数是将func作用于se ...

  7. Luogu P3957 跳房子

    题面 跳房子,也叫跳飞机,是一种世界性儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下:  在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一条直线上.每个格子内有一 ...

  8. Bootstrap fileinput.js,最好用的文件上传组件

    本篇介绍如何使用bootstrap fileinput.js(最好用的文件上传组件)来进行图片的展示,上传,包括springMVC后端文件保存. 一.demo   二.插件引入 <link ty ...

  9. react + redux 完整的项目,同时写一下个人感悟

    先附上项目源码地址和原文章地址:https://github.com/bailicangd... 做React需要会什么? react的功能其实很单一,主要负责渲染的功能,现有的框架,比如angula ...

  10. df -h命令卡死解决办法

    1.现象 同事突然反应说有个服务器进入/目录运行 ls  -l 无反应,同时运行df  -h也卡死了.如果你的机器有用到nfs请直接看第四大点. 2.分析 运行mount [conversant@sw ...