触发 SIGTERM 事件时,我无法退出我的 main.go 应用程序。
switch 语句中的 SIGTERM 情况不会被调用,并且“触发”不会被打印出来。
这是我的代码
func main() {
port := os.Getenv("PORT")
fmt.Printf("Started\n")
if port == "" {
port = "8080"
}
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-signalChannel
switch sig {
case os.Interrupt:
//nothing yet
case syscall.SIGTERM:
fmt.Printf("triggered") //never gets called
}
}()
http.HandleFunc("/", HelloServer)
http.ListenAndServe(":" + port, nil)
}
我尝试了以下解决方案,但无法正常工作。