我正在尝试编写一个测试来消除终止命令的功能。为此,我需要测试的目标(一个非常简单的 go http 服务器 - https://github.com/jadekler/git-grunt-gostop/blob/master/test/fixtures/gostop_basic.go)来区分一直用-2 vs -9 杀人。有什么方法可以做到这一点吗?似乎无论 -2 与 -9 的情况如何,go 的 defer 都不会发生,这是我的第一次尝试。随后的研究并没有带来启发。
感谢您的任何建议!
您可以使用该os/signal包捕获信号。你可以SIGINT用这样的代码来捕获:
import (
"os"
"os/signal"
)
...
// the signal module will lose notifications if the channel is not ready to receive, so give it a buffer
ch := make(chan os.Signal, 5)
go func() {
for sig := range ch {
// interrupt signal received
os.Exit(0)
}
}()
signal.Notify(ch, os.SIGINT)
Run Code Online (Sandbox Code Playgroud)
不幸的是,您不能对 执行同样的操作SIGKILL。从signal(7)手册页:
这些信号
SIGKILL无法SIGSTOP被捕获、阻止或忽略。
| 归档时间: |
|
| 查看次数: |
256 次 |
| 最近记录: |