Golang os/signal.Stop() and Notify() functions example
package os/signal
Golang os/signal.Stop() and Notify() functions usage example
log.Println("Server started. Press Ctrl-C to stop server")
// Catch the Ctrl-C and SIGTERM from kill command
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
signalType := <-ch
signal.Stop(ch)
log.Println("Exit command received. Exiting...")
// this is a good place to flush everything to disk
// before terminating.
log.Println("Signal type : ", signalType)
os.Exit(0)
}()
See full example at : How to intercept Ctrl-C interrupt or kill signal and determine the signal type tutorial.
References :
Advertisement
Something interesting
Tutorials
+16.8k Golang : read gzipped http response
+20.3k Golang : Check if os.Stdin input data is piped or from terminal
+30.6k Golang : Remove characters from string example
+7.8k Swift : Convert (cast) String to Double
+7.1k Golang : Gorrila mux.Vars() function example
+13.6k Golang : Qt progress dialog example
+15.8k Golang : Get digits from integer before and after given position example
+31.6k Golang : Get local IP and MAC address
+8.3k Golang : Implementing class(object-oriented programming style)
+13.9k Golang : How to check if a file is hidden?
+6.1k Golang : Missing Subversion command
+13.5k Golang : Read XML elements data with xml.CharData example