feat: add signal handler

This commit is contained in:
Aliberk Sandıkçı 2023-07-31 16:20:07 +03:00
parent 8b63561bef
commit c91ea933cd
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
2 changed files with 37 additions and 0 deletions

View file

@ -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")

View 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)
}