Created
January 19, 2018 15:32
-
-
Save maierru/345af09cd43b86418686edb986eb79a5 to your computer and use it in GitHub Desktop.
go systems signals handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func sygnalsWait() { | |
pid := os.Getpid() | |
fmt.Printf("pid: %v", pid) | |
SigChannel := make(chan os.Signal, 1) | |
done := make(chan int, 1) | |
// kill pid | |
signal.Notify(SigChannel, syscall.SIGTERM) | |
// kill -s USR1 pid | |
signal.Notify(SigChannel, syscall.SIGUSR1) | |
// kill -s USR2 | |
signal.Notify(SigChannel, syscall.SIGUSR2) | |
go func() { | |
for { | |
s := <-SigChannel | |
switch s { | |
case syscall.SIGUSR1: | |
fmt.Printf("\nReceived signal %v", s) | |
case syscall.SIGUSR2: | |
fmt.Printf("\nReceived signal %v", s) | |
default: | |
fmt.Printf("\nExiting %v after 3s", s) | |
time.Sleep(3 * time.Second) | |
done <- 0 | |
} | |
} | |
}() | |
<-done | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment