diff --git a/go-daemon/godaemon1.go b/go-daemon/godaemon1.go index 9b42d43..e9fc9ff 100644 --- a/go-daemon/godaemon1.go +++ b/go-daemon/godaemon1.go @@ -3,6 +3,8 @@ package main import ( "godaemon1/service" "log" + "os" + "os/signal" "time" "github.com/sevlyar/go-daemon" @@ -27,7 +29,16 @@ func main() { } defer cntxt.Release() - service.SigHandler() + // TODO make both in external package service these lines but also async + sigChannel := make(chan os.Signal, 1) + signal.Notify(sigChannel) + + go func() { + for { + s := <-sigChannel + service.SigHandler(s) + } + }() log.Print("- - - - - - - - - - - - - - -") log.Print("daemon started") diff --git a/go-daemon/service/sighandler.go b/go-daemon/service/sighandler.go index 965943e..ed5865d 100644 --- a/go-daemon/service/sighandler.go +++ b/go-daemon/service/sighandler.go @@ -3,32 +3,17 @@ package service import ( "log" "os" - "os/signal" "syscall" ) -func handler(signal os.Signal) { +func SigHandler(signal os.Signal) { if signal == syscall.SIGTERM { log.Println("Got signal: SIGTERM") } else if signal == syscall.SIGINT { log.Println("Got signal: SIGINT") + } else if signal == syscall.SIGKILL { + log.Println("Got signal: SIGINT") } else { - log.Println("Got another signal") + log.Printf("Got another signal: %v\n", signal) } } - -func SigHandler() { - sigChannel := make(chan os.Signal, 1) - signal.Notify(sigChannel) - exitChannel := make(chan int) - - go func() { - for { - s := <-sigChannel - handler(s) - } - }() - - exitcode := <-exitChannel - os.Exit(exitcode) -}