-
Notifications
You must be signed in to change notification settings - Fork 0
/
18fsnotify.go
59 lines (56 loc) · 1.22 KB
/
18fsnotify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"os/exec"
"path/filepath"
)
func main() {
// 创建文件/目录监听器
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("watcher new err: [%+v]", err)
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// 打印监听事件
log.Print("event:", event)
if event.Op&fsnotify.Create == fsnotify.Create {
// 获取新文件完整路径
filePath, err := filepath.Abs(event.Name)
if err != nil {
log.Errorf("Error: %v", err)
continue
}
// 执行shell命令
cmd := exec.Command("php", "/opt/www/yiic", "handleevidnotice", "--file="+filePath)
err = cmd.Run()
if err != nil {
log.Errorf("Error: %v", err)
continue
}
log.Println("Executed shell command for", filePath)
}
// 可以启动新的goroutine或使用channel进行事件传递
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
// 监听当前目录
err = watcher.Add("/data/alarm")
if err != nil {
log.Fatal("err add dir: [%+v]", err)
}
<-done
}