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 :

http://golang.org/pkg/os/signal/#Stop

http://golang.org/pkg/os/signal/#Notify

Advertisement