douyou7072 2017-06-16 20:32
浏览 263

Golang:使用exec.ExitError杀死os.Process

If I have a os.Exec object called "myCmd" and I call myCmd.Process.Kill(), what is the return code behavior of the process? Will it return a exec.ExitError? I want to forcefully kill the os.Exec process (i.eo kill -9), have it return a exec.ExitError or something that my goroutine can distinguish for a normal cmd exit with return code 0.

What I have so far:

myCmd.Start()

var cmdWatcher = func(childCmd os.Cmd) {

    err := childCmd.Wait()
    if exitErr, k := err.(*exec.ExitError); k {
        fmt.Print("ExitError detected")
    }
    return 
}

go cmdWatcher(myCmd)

myCmd.Process.Kill()
  • 写回答

1条回答 默认 最新

  • douwanc63652 2017-06-16 20:47
    关注

    Kill() is the same as calling kill -9 on the process, it sends a SIGKILL, which cannot be caught. As with all non-zero exit codes, Wait() will then return an ExitError.

    You also have the option of using Process.Signal(), which alloww you to specify any signal you want (for example, SIGINT or SIGTERM instead). Unfortunately, it doesn't look like the os.ExitError type allows you to retrieve the exit code itself as anything except the exit status string. However, you can still use the presence or absence of that error type as indication of non-zero or zero exit status.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?