下边测试的例子中,有文件变化时,case event := <-Watch.Events会触发三次,有用过的朋友能解释一下啊
package main
import (
"flag"
"log"
"os/exec"
"strings"
"time"
// "sync"
// "time"
"github.com/fsnotify/fsnotify"
)
var (
sleeptime int
path string
cmd string
args []string
)
func init() {
flag.IntVar(&sleeptime, "t", 30, "-t=30")
flag.StringVar(&path, "p", "./", "-p=filepath or dirpath")
flag.StringVar(&cmd, "c", "", "-c=command")
str := flag.String("a", "", `-a="args1 args2"`)
flag.Parse()
args = strings.Split(*str, " ")
}
func main() {
Watch, err := fsnotify.NewWatcher()
if err != nil {
log.Println("Init monitor error: ", err.Error())
return
}
if err := Watch.Add(path); err != nil {
log.Println("Add monitor path error: ", path)
return
}
var (
// cron bool = false
// lock = new(sync.Mutex)
)
for {
select {
case event := <-Watch.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Printf("Monitor event %s", event.String())
}
// if !cron {
// cron = true
// go func() {
// T := time.After(time.Second * time.Duration(sleeptime))
// <-T
// if err := call(cmd, args...); err != nil {
// log.Println(err)
// }
// lock.Lock()
// cron = false
// lock.Unlock()
// }()
// }
case err := <-Watch.Errors:
log.Println(err)
return
}
}
}
func call(programe string, args ...string) error {
cmd := exec.Command(programe, args...)
buf, err := cmd.Output()
if err != nil {
return err
}
log.Printf("\n%s\n", string(buf))
return nil
}
有疑问加站长微信联系(非本文作者)