Created
August 26, 2020 06:17
-
-
Save shenjing023/d0f4a5d405c79cc65377fb0e6a2e0ebd to your computer and use it in GitHub Desktop.
[Golang] os/exec 带超时 command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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