-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (76 loc) · 1.72 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// +build linux
package main
import (
"errors"
"log"
"os"
ps "github.com/mitchellh/go-ps"
"github.com/rjeczalik/notify"
)
var (
directory string
)
func isDirectory(directoryPath string) (bool, error) {
path, err := os.Stat(directoryPath)
switch {
case err != nil:
log.Fatal(err)
case path.IsDir():
return true, nil
}
return false, err
}
func FindProcess(key string) (int, string, error) {
pname := ""
pid := 0
err := errors.New("not found")
ps, _ := ps.Processes()
for i, _ := range ps {
if ps[i].Executable() == key {
pid = ps[i].Pid()
pname = ps[i].Executable()
err = nil
break
}
}
return pid, pname, err
}
func hupProcess(pid int, s string) error {
log.Printf("HUPing process %s with PID of %d", s, pid)
proc, err := os.FindProcess(pid)
if err != nil {
log.Fatal(err)
}
proc.Signal(os.Interrupt)
return err
}
func main() {
directory, ok := os.LookupEnv("WATCH_DIRECTORY")
if !ok {
log.Fatal("Could not find WATCH_DIRECTORY environment vairable, exiting.")
}
hupTarget, ok := os.LookupEnv("HUP_PROCESS_NAME")
if !ok {
log.Fatal("Could not find HUP_PROCESS_NAME environment vairable, exiting.")
}
log.Printf("Watching directory %s and will HUP %s", directory, hupTarget)
hupTarget = hupTarget[0:15]
_, err := isDirectory(directory)
if err != nil {
log.Fatal(err)
}
for {
c := make(chan notify.EventInfo, 1)
if err := notify.Watch(directory, c, notify.InCloseWrite, notify.InMovedTo); err != nil {
log.Fatal(err)
}
defer notify.Stop(c)
switch ei := <-c; ei.Event() {
case notify.InCloseWrite, notify.InMovedTo:
log.Printf("Found file %s", ei.Path())
if pid, name, err := FindProcess(hupTarget); err == nil {
hupProcess(pid, name)
}
}
}
}