Skip to content

Instantly share code, notes, and snippets.

@Allenxuxu
Created November 21, 2019 13:29
Show Gist options
  • Save Allenxuxu/da7670447bf732a2fc7f144d583f7fea to your computer and use it in GitHub Desktop.
Save Allenxuxu/da7670447bf732a2fc7f144d583f7fea to your computer and use it in GitHub Desktop.
golang 监听信号优雅退出
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
// 监听退出信号
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
log.Println("程序启动")
_ = <-shutdown
// TODO 清理操作
time.Sleep(time.Second * 2)
log.Println("程序退出")
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment