Created
January 19, 2016 04:03
-
-
Save diabloneo/130317e801984fa0953f to your computer and use it in GitHub Desktop.
Execute command with timeout
This file contains 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 ( | |
"fmt" | |
"os/exec" | |
"time" | |
) | |
func main() { | |
cmd := exec.Command("./pause1min") | |
cmd.Start() | |
resultChan := make(chan error) | |
go func() { | |
resultChan <- cmd.Wait() | |
}() | |
WaitForResult: | |
for { | |
select { | |
case err := <-resultChan: | |
fmt.Print() | |
fmt.Println("Command result:", err) | |
break WaitForResult | |
case <-time.After(15 * time.Second): | |
fmt.Println("Timeout") | |
err := cmd.Process.Kill() | |
fmt.Println(err) | |
break WaitForResult | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment