Created
October 3, 2012 02:26
-
-
Save nesv/3824578 to your computer and use it in GitHub Desktop.
Mainloop for Go apps that catch OS signals
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
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