Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
Problem :
Your server needs to perform some clean up operation or flush everything in memory to disk before shutting down. How to intercept Ctrl-C,interrupt or kill signal in your program and perform the tasks? How to determine the signal type that your program received?
Solution :
Use the os/signal.Notify()
function to intercept the signals and perform the required tasks before shutting down.
For example : (interrupt.go)
package main
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
html := `Hello
World!`
w.Write([]byte(html))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", SayHelloWorld)
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)
}()
log.Println(http.ListenAndServe(":8080", mux))
}
Run the code above with these commands :
>go run interrupt.go
and press Ctrl-C.
2015/06/23 11:40:43 Server started. Press Ctrl-C to stop server
^C2015/06/23 11:40:45 Exit command received. Exiting...
2015/06/23 11:42:45 Signal type : interrupt
or
>go run interrupt.go &
(no applicable to Windows)
You will see program goes to background. Find out the process ids with the ps -ef | grep interrupt
command.
>ps -ef | grep interrupt
root 30551 30306 1 11:41 pts/0 00:00:00 go run interrupt.go
root 30559 30551 0 11:41 pts/0 00:00:00 /tmp/go-build908833771/command-line-arguments/_obj/exe/interrupt
In this example, I would issue the command
>kill 30559 30551
and a similar output will be produced except that this time, the signal type is terminated
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.4k Golang : Append content to a file
+7.1k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+10.1k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+13.8k Android Studio : Use image as AlertDialog title with custom layout example
+13.6k Golang : concatenate(combine) strings
+21.4k Golang : Join arrays or slices example
+13.3k Golang : Check if an integer is negative or positive
+7k Golang : Scanf function weird error in Windows
+5.1k Python : Delay with time.sleep() function example
+17.5k Golang : Put UTF8 text on OpenCV video capture image frame
+7.6k Golang : Check from web if Go application is running or not
+4.7k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version