(摘) Golang Shell命令中止信号

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

运行一个指令,并在几秒后中止它。

package main

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

func main() {
    cmd := exec.Command("arecord", "1.wav")
    start := time.Now()
    time.AfterFunc(10*time.Second, func() {
      //cmd.Process.Kill()
      cmd.Process.Signal(syscall.SIGINT)
    })
    err := cmd.Run()
    fmt.Printf("pid=%d duration=%s err=%s\n", cmd.Process.Pid, time.Since(start), err)
}

相关文章