Created
April 12, 2018 13:27
-
-
Save ipedrazas/86e275472d376be9dbf06a7b9eeeaf09 to your computer and use it in GitHub Desktop.
signal hander to gracefully exit in Go
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
// setup a signal hander to gracefully exit | |
func sigHandler() <-chan struct{} { | |
stop := make(chan struct{}) | |
go func() { | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, | |
syscall.SIGINT, // Ctrl+C | |
syscall.SIGTERM, // Termination Request | |
syscall.SIGSEGV, // FullDerp | |
syscall.SIGABRT, // Abnormal termination | |
syscall.SIGILL, // illegal instruction | |
syscall.SIGFPE) // floating point - this is why we can't have nice things | |
sig := <-c | |
glog.Warningf("Signal (%v) Detected, Shutting Down", sig) | |
close(stop) | |
}() | |
return stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment