Windows上除SIGKILL以外的其他信号无法终止进程 [英] Signal other than SIGKILL not terminating process on Windows

查看:75
本文介绍了Windows上除SIGKILL以外的其他信号无法终止进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Go启动一个简单的Java应用程序,目的是证明Go可以发送诸如SIGQUIT或SIGTERM之类的信号,而Java可以捕获并适当地处理它(即正常关闭).当我在命令行上运行Java程序并向其发送CTRL + C时,Java程序会正确捕获信号.

I am launching a simple Java application through Go, with the goal of proving that Go can send a signal like SIGQUIT or SIGTERM, and the Java can catch that and handle it appropriately (i.e. shutdown gracefully). When I run the Java program on the command line and send it a CTRL+C, the Java program correctly catches the signal.

但是,当我通过Go启动Java程序并尝试向进程发送信号时,Java进程既不终止也不处理信号.唯一起作用的是SIGKILL,它当然不会被捕获,只会杀死进程.

However, when I launch the Java program through Go and attempt to send the process a signal, the Java process is neither terminated nor does it handle the signal. The only one that works is SIGKILL, which of course is not caught, and simply kills the process.

以下是我的Go代码的相关部分:

Here are the relevant parts of my Go code:

开始:

startChan := make(chan bool)
go func(startChan chan<- bool) {
    a.Cmd = exec.Command("java", "-jar", "C:\\Code\\sigterm\\TestApp.jar")
    a.Cmd.Stdout = os.Stdout
    a.Cmd.Stderr = os.Stderr

    launchErr := a.Cmd.Start()
    if launchErr != nil {
        fmt.Println("Unable to start app:" + launchErr.Error())
    }
    startChan <- true
}(startChan)

停止:

func (a *App) Stop(timeout time.Duration) {
  a.Cmd.Process.Signal(syscall.SIGQUIT) //Does not work
  a.Cmd.Process.Signal(syscall.SIGTERM) //Does not work
  a.Cmd.Process.Signal(syscall.SIGKILL) //Works
}

顺便说一句,这是在Windows上.我通过启动另一个程序(记事本)进行了尝试,并得到了相同的结果.也就是说,只有SIGKILL可以工作.我已经确认Go可以获取正确的PID,依此类推,因此信号应该发送到正确的位置.

This is on Windows, by the way. I tried it by launching another program (notepad) and got the same results; that is, only SIGKILL worked. I have confirmed that Go is getting the correct PID and so forth, so the signal should be going to the right place.

关于如何使它工作的任何想法?

Any ideas about how I can get this to work?

推荐答案

似乎不支持此说明,如文档中所述:

This does not seem to be supported as stated in the documentation:

https://godoc.org/os#Process.Signal

未实现Windows上的发送中断.

Sending Interrupt on Windows is not implemented.

此外,请参阅此问题讨论以获取有关为何无法完成操作的更多背景信息:

Also, see this issue discussion to get more context on why it could not be done:

https://github.com/golang/go/issues/6720

这篇关于Windows上除SIGKILL以外的其他信号无法终止进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆