duannuochi3549 2014-07-27 16:13
浏览 97
已采纳

子进程上的Process.Kill()

I'm trying to stop the process started with exec.Command("go", "run", "server.go") and all its child processes.

But when I call cmd.Process.Kill() and the go process stops, the child process (a web server in server.go) continues to run.

package main

import (
    "fmt"
    "os/exec"
    "time"
)

func run() *exec.Cmd {
    cmd := exec.Command("go", "run", "server.go")

    err := cmd.Start()
    if err != nil {
        panic(err)
    }

    return cmd
}

func main() {
    cmd := run()

    time.Sleep(time.Second * 2)

    err := cmd.Process.Kill()
    if err != nil {
        panic(err)
    }
    cmd.Process.Wait()

    // === Web server is still running! ===

    fmt.Scanln()
}

It looks like Process.Kill() only stops the go (run) process, but leaves its child process (web server) running.

^C kills the whole process group, including all child (and sub-child) processes. How can I do the same?

I tried cmd.Process.Signal(os.Interrupt), syscall.SIGINT, syscall.SIGQUIT and syscall.SIGKILL, none of which did anything.

  • 写回答

1条回答 默认 最新

  • drr7731 2014-07-27 21:10
    关注

    Don't use the go run command. Use the go install command to install your packages and programs and then execute your program.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog