Skip to content

Instantly share code, notes, and snippets.

@inhies
Last active December 14, 2015 11:38
Show Gist options
  • Save inhies/5080024 to your computer and use it in GitHub Desktop.
Save inhies/5080024 to your computer and use it in GitHub Desktop.
Snippet showing how to catch common interrupts and use the HUP signal to reload your config on the fly. Note that you can not catch SIGKILL so we don't even try.
c := make(chan os.Signal, 1)
signal.Notify(c)
go func() {
for sig := range c {
switch sig {
case syscall.SIGHUP:
fmt.Println("Reloading config")
if err := parseConfig(); err != nil {
fmt.Println("Error parsing config")
fmt.Println(err)
}
case syscall.SIGTERM:
os.Exit(1) // Error becuase we were killed
case syscall.SIGINT:
// Do stuff and gracefully shutdown
os.Exit(0)
}
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment