我明白如何捕捉每一个信号
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)
}
}()
一般来说我只是想
- 处理一些可以理解的信号 - X
- 记录我收到的每个信号 - Y
- 不改变 Y - X 信号的行为
最佳答案
Notify
禁用一组给定异步信号的默认行为,而是通过一个或多个已注册 channel 传递它们。我认为这意味着您无法在不改变信号行为的情况下拦截信号。
docs描述默认行为:
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
应该可以捕获信号、处理它,然后模拟适合该信号的默认行为。
关于go - 是否可以只记录收到的每个信号而不改变行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235615/