增加 pid文件进程感知

This commit is contained in:
dengsgo 2020-06-27 15:11:55 +08:00
parent 7c6f8f910b
commit c5f0649ab5
2 changed files with 15 additions and 0 deletions

View File

@ -48,3 +48,9 @@ func stopDaemon() error {
os.Remove(getPidFile())
return nil
}
func stopSelf() {
pid := os.Getpid()
os.Remove(getPidFile())
_ = exec.Command("kill", strconv.Itoa(pid)).Run()
}

View File

@ -200,6 +200,15 @@ func initWatcher() {
}
func watchChangeHandler(event fsnotify.Event) {
// stop the fileboy daemon process when the .fileboy.pid file is changed
if event.Name == getPidFile() &&
(event.Op == fsnotify.Remove ||
event.Op == fsnotify.Write ||
event.Op == fsnotify.Rename) {
logUInfo("exit daemon process")
stopSelf()
return
}
if event.Op != fsnotify.Create && event.Op != fsnotify.Rename {
return
}