Skip to content

Instantly share code, notes, and snippets.

@ipedrazas
Created April 12, 2018 13:27
Show Gist options
  • Save ipedrazas/86e275472d376be9dbf06a7b9eeeaf09 to your computer and use it in GitHub Desktop.
Save ipedrazas/86e275472d376be9dbf06a7b9eeeaf09 to your computer and use it in GitHub Desktop.
signal hander to gracefully exit in Go
// 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