Created
October 14, 2019 01:47
-
-
Save guanhui07/88678208bbfee430d4d1ff4e91d549c4 to your computer and use it in GitHub Desktop.
go捕获Ctrl+C信号
This file contains hidden or 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 ( | |
"os" | |
"os/signal" | |
"syscall" | |
"fmt" | |
) | |
func main() { | |
sigs := make(chan os.Signal, 1) | |
done := make(chan bool, 1) | |
//设置要接收的信号 | |
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | |
go func() { | |
sig := <-sigs | |
fmt.Println() | |
fmt.Println(sig) | |
done <- true | |
}() | |
fmt.Println("等待信号") | |
<-done | |
fmt.Println("进程被终止") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment