Skip to content

Instantly share code, notes, and snippets.

@shenjing023
Created August 26, 2020 06:17
Show Gist options
  • Select an option

  • Save shenjing023/d0f4a5d405c79cc65377fb0e6a2e0ebd to your computer and use it in GitHub Desktop.

Select an option

Save shenjing023/d0f4a5d405c79cc65377fb0e6a2e0ebd to your computer and use it in GitHub Desktop.
[Golang] os/exec 带超时 command
package main
import (
"log"
"os/exec"
"time"
)
func main() {
cmd := exec.Command("ping", "www.baidu.com")
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
killer := time.AfterFunc(time.Second*3, func() {
cmd.Process.Kill()
})
defer killer.Stop()
cmd.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment