From a76d4cfe6cad64bb5b340e07360891039cc2daf0 Mon Sep 17 00:00:00 2001 From: dengsgo Date: Sun, 19 Jul 2020 18:06:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E5=AF=B9=E4=BF=A1?= =?UTF-8?q?=E5=8F=B7=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fileboy.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/fileboy.go b/fileboy.go index 4bc5079..496c1e2 100644 --- a/fileboy.go +++ b/fileboy.go @@ -6,9 +6,11 @@ import ( "log" "math/rand" "os" + "os/signal" "path" "strconv" "strings" + "syscall" "time" "gopkg.in/fsnotify/fsnotify.v1" @@ -319,6 +321,20 @@ func parseArgs() { } } +func signalHandler() { + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + if taskMan != nil && taskMan.cmd != nil && taskMan.cmd.Process != nil { + if err := taskMan.cmd.Process.Kill(); err != nil { + logError("stopping the process failed: PID:", taskMan.cmd.ProcessState.Pid(), ":", err) + } + } + os.Exit(0) + }() +} + func getFileGirlPath() string { return projectFolder + "/" + filegirlYamlName } @@ -341,5 +357,6 @@ func main() { if err != nil { logAndExit(err) } + signalHandler() parseArgs() }