Graceful Exit in Go Program
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
/* | |
* Killing a program immediately sometimes leads to corrupted data or files, which brings unexpected result in your system. | |
* In Go, you can catch the termination signals and let your program decide the time of exiting. | |
*/ | |
package main | |
import ( | |
"os" | |
"os/signal" | |
"syscall" | |
) | |
func main() { | |
println("Notify the signals ...") | |
sigs := make(chan os.Signal, 1) | |
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) | |
nsig := <-sigs | |
println("... Exit with: ", nsig.String()) | |
signal.Stop(sigs) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment