golang handling kill in a process started by cmd.Start

huangapple go评论84阅读模式
英文:

golang handling kill in a process started by cmd.Start

问题

我有两个Go程序。ProgA使用cmd.Start()启动ProgB。在ProgA中,我尝试杀死ProgB,但是ProgB不应立即被杀死,它必须在死亡之前进行一些清理工作。因此,我在ProgB中使用signal.Notify来处理sigcall.SIGKILL,但是每当ProgA调用progb.Process.Kill()时,似乎没有通知到ProgB(将内容写入sigc通道)。

在ProgB中,我有以下通知代码:

signal.Notify(sigc, syscall.SIGKILL)
go func() {
			fmt.Println("开始监听")
            <-sigc
	    	fmt.Println("收到信号")
            cleanUp()
            os.Exit(1)   
			
}()
someLongRunningCode()

我是否遗漏了什么?我确定ProgA发送了SIGKILL,因为cmd.Process.Kill()在内部执行了process.Signal(SIGKILL)。

英文:

I have two go programs. ProgA starts ProgB using cmd.Start(). From ProgA I try to kill ProgB, but ProgB shouldn't get killed immediately, it has to do some cleanup before dying. So I'm using signal.Notify in ProgB to handle sigcall.SIGKILL but whenever ProgA calls progb.Process.Kill() it doesn't seem to notify ProgB(write contents to sigc channel)

in ProgB I have the notify like this:

signal.Notify(sigc, syscall.SIGKILL)
go func() {
			fmt.Println(&quot;started listening&quot;)
            &lt;-sigc
	    	fmt.Println(&quot;sig term&quot;)
            cleanUp()
            os.Exit(1)   
			
}()
someLongRunningCode() 

is there something I'm missing out? I'm sure that ProgA sends a SIGKILL because cmd.Process.Kill() internally does a process.Signal(SIGKILL)

答案1

得分: 7

SIGKILL无法被接收进程捕获 - 内核将强制终止进程。您可以向进程发送SIGTERM并在另一端处理它 - 这是一种常规的停止应用程序的方法。

英文:

SIGKILL cannot be trapped by recieving process - kernel will force process termination. You may send SIGTERM to process and handle it on other side - it is a conventional method to stop an application.

huangapple
  • 本文由 发表于 2014年8月18日 15:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25358162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定