feat: add signal handler
This commit is contained in:
parent
8b63561bef
commit
c91ea933cd
2 changed files with 37 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"godaemon1/service"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
|
@ -26,6 +27,8 @@ func main() {
|
|||
}
|
||||
defer cntxt.Release()
|
||||
|
||||
service.SigHandler()
|
||||
|
||||
log.Print("- - - - - - - - - - - - - - -")
|
||||
log.Print("daemon started")
|
||||
|
||||
|
|
34
go-daemon/service/sighandler.go
Normal file
34
go-daemon/service/sighandler.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func handler(signal os.Signal) {
|
||||
if signal == syscall.SIGTERM {
|
||||
log.Println("Got signal: SIGTERM")
|
||||
} else if signal == syscall.SIGINT {
|
||||
log.Println("Got signal: SIGINT")
|
||||
} else {
|
||||
log.Println("Got another 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)
|
||||
}
|
Loading…
Reference in a new issue