Skip to content

Instantly share code, notes, and snippets.

@nesv
Created October 3, 2012 02:26
Show Gist options
  • Save nesv/3824578 to your computer and use it in GitHub Desktop.
Save nesv/3824578 to your computer and use it in GitHub Desktop.
Mainloop for Go apps that catch OS signals
import (
"log"
"os"
"os/signal"
"syscall"
)
var (
SystemSignals = make(chan os.Signal)
)
func StartSignalListener() {
signal.Notify(SystemSignals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGHUP)
for {
sig := <-SystemSignals
if sig == syscall.SIGINT {
log.Println("caught SIGINT; exiting")
os.Exit(0)
} else if sig == syscall.SIGKILL {
log.Println("received SIGKILL; exiting")
os.Exit(0)
} else if sig == syscall.SIGHUP {
log.Println("received SIGHUP; reloading...")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment