go - 是否可以只记录收到的每个信号而不改变行为?

标签 go signals

我明白如何捕捉每一个信号

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)
    }
}()

一般来说我只是想

  1. 处理一些可以理解的信号 - X
  2. 记录我收到的每个信号 - Y
  3. 不改变 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/

相关文章:

linux - 为什么我遇到 Linux 信号处理的意外行为?

go - 使用 GO 进行 AWS SNS 签名验证

go - 引用库中的另一个.go文件

oop - Go 中是否存在脆弱的基类问题?

c++ - 为什么连接的信号有参数但插槽没有参数?

ruby - 如何在 Ruby 进程中捕获信号

go - Go中如何选择?

转到 pprof : Got Error unrecognized profile format

go - SIGKILL 以外的信号不会在 Windows 上终止进程

c - Linux 中的ighandler_t typedef 语句?