Created
November 21, 2019 13:29
-
-
Save Allenxuxu/da7670447bf732a2fc7f144d583f7fea to your computer and use it in GitHub Desktop.
golang 监听信号优雅退出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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