有没有办法在 Golang 中终止以 os.exec 启动的进程?例如(来自http://golang.org/pkg/os/exec/#example_Cmd_Start),
cmd := exec.Command("sleep", "5") err := cmd.Start() if err != nil { log.Fatal(err) } log.Printf("Waiting for command to finish...") err = cmd.Wait() log.Printf("Command finished with error: %v", err)
有没有办法提前终止该进程,也许是在 3 秒后?
提前致谢
运行并终止一个exec.Process:
exec.Process
// Start a process: cmd := exec.Command("sleep", "5") if err := cmd.Start(); err != nil { log.Fatal(err) } // Kill it: if err := cmd.Process.Kill(); err != nil { log.Fatal("failed to kill process: ", err) }
exec.Process超时后运行并终止:
ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second) defer cancel() if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil { // This will fail after 3 seconds. The 5 second sleep // will be interrupted. }
请参阅Go 文档中的此示例
遗产
在 Go 1.7 之前,我们没有这个context包,这个答案是不同的。
context
exec.Process使用通道和 goroutine 在超时后运行和终止:
// Start a process: cmd := exec.Command("sleep", "5") if err := cmd.Start(); err != nil { log.Fatal(err) } // Wait for the process to finish or kill it after a timeout (whichever happens first): done := make(chan error, 1) go func() { done <- cmd.Wait() }() select { case <-time.After(3 * time.Second): if err := cmd.Process.Kill(); err != nil { log.Fatal("failed to kill process: ", err) } log.Println("process killed as timeout reached") case err := <-done: if err != nil { log.Fatalf("process finished with error = %v", err) } log.Print("process finished successfully") }
要么进程结束并接收到它的错误(如果有),done要么已经过去了 3 秒并且程序在完成之前被终止。
done