From c5f0649ab5a3a1197f9f369ef3811020dbe2f086 Mon Sep 17 00:00:00 2001 From: dengsgo Date: Sat, 27 Jun 2020 15:11:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20pid=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E6=84=9F=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daemon.go | 6 ++++++ fileboy.go | 9 +++++++++ 2 files changed, 15 insertions(+) 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 }