Golang如何捕获SIGTERM信号并继续应用程序。

7

在 Golang 中,是否有可能像处理 panic/defer 那样捕获 sigterm 并继续执行代码?

示例:

func main() {
    fmt.Println("app started")
    setupGracefulShutdown()

    for {
    }
    close()    
}

func close() {
    fmt.Println("infinite loop stopped and got here")
}

func setupGracefulShutdown() {
    sigChan := make(chan os.Signal)
    signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

    go func() {
        fmt.Println(" got interrupt signal: ", <-sigChan)
    }()
}

// "app started"
// CTRL + C
// ^C "got interrupt signal:  interrupt"
// app don't stop

我想要的是打印出“无限循环停止并到达此处”,然后结束应用程序。
// "app started"
// CTRL + C
// ^C "got interrupt signal:  interrupt"
// "infinite loop stopped and got here"

2
你的“无限”循环可以监视sigChan,并在接收到值后“中断”。这有什么问题吗? - icza
附注:为您的信号创建一个容量为1的通道。 - Volker
1
该示例仅用于可视化,我无法控制无限循环,我正在尝试在内部库中实现此功能。 - Cae Vecchi
1个回答

12

这很容易实现。因为信号通道需要阻塞并等待信号,所以您必须在不同的 goroutine 中启动业务逻辑代码。

func main() {
    cancelChan := make(chan os.Signal, 1)
    // catch SIGETRM or SIGINTERRUPT
    signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
    go func() {
        // start your software here. Maybe your need to replace the for loop with other code
        for {
            // replace the time.Sleep with your code
            log.Println("Loop tick")
            time.Sleep(time.Second)
        }
    }()
    sig := <-cancelChan
    log.Printf("Caught signal %v", sig)
    // shutdown other goroutines gracefully
    // close other resources
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接