diff --git a/daemon.go b/daemon.go index 4de9932..f06c74d 100644 --- a/daemon.go +++ b/daemon.go @@ -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() +} diff --git a/fileboy.go b/fileboy.go index c837d28..4bc5079 100644 --- a/fileboy.go +++ b/fileboy.go @@ -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 }