I understand how to catch every signal
idleConnsClosed := make(chan bool)
SignalChannel := make(chan os.Signal, 1)
// Notify to SignalChannel any signal received
signal.Notify(SignalChannel)
go func() {
for {
sig := <-SignalChannel
log.Notice("Signal %#v received", sig)
switch sig {
case syscall.SIGTERM:
// ok sigterm, I know how to handle with it
log.Info("ShutDown HTTP server (SIGTERM)")
if err := server.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
log.Error("HTTP server Shutdown: %v", err)
}
case syscall.SIGHUP:
//reinit configurations and do some stuff, I know how to handle this
continue
case syscall.SIGPIPE:
//Don't know what to do, just wanted to log it
continue
case syscall.SIGABRT:
//exit with core dump...how to do that ?
continue
default:
// unhandled signal?, ok how to not change it's behavior?
log.Warning("Unhandled Signal %s received!", sig)
}
close(idleConnsClosed)
}
}()
In general I just want to
i think you want below code. The code has only a skeleton.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
done := make(chan bool)
channel := make(chan os.Signal, 1)
signal.Notify(channel)
go func() {
for sig := range channel {
switch sig {
case syscall.SIGTERM:
fmt.Printf("SIGTERM Signal %s received!
", sig)
case syscall.SIGHUP:
fmt.Printf("SIGHUP Signal %s received!
", sig)
case syscall.SIGPIPE:
fmt.Printf("SIGPIPE Signal %s received!
", sig)
case syscall.SIGABRT:
fmt.Printf("SIGABRT Signal %s received!
", sig)
default:
fmt.Printf("Unhandled Signal %s received!
", sig)
done <- true
}
}
}()
<-done
}
code updated.
Notify
disables the default behaviour for a given set of asynchronous signals and instead delivers them over one or more registered channels. I think that means that you can't intercept the signal without changing its behaviour.
The docs describe default behaviour:
By default, a synchronous signal is converted into a run-time panic. A SIGHUP, SIGINT, or SIGTERM signal causes the program to exit. A SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGSTKFLT, SIGEMT, or SIGSYS signal causes the program to exit with a stack dump
It should be possible to capture the signal, handle it, then emulate default behaviour appropriate to that signal.