3 回答

TA貢獻(xiàn)1836條經(jīng)驗 獲得超13個贊
終止運(yùn)行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超時后終止運(yùn)行:
// 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")
}
該過程結(jié)束并且在done3秒鐘內(nèi)收到了錯誤(如果有的話),并且該程序在完成之前被終止了。
- 3 回答
- 0 關(guān)注
- 291 瀏覽
添加回答
舉報