Skip to content

Instantly share code, notes, and snippets.

@diabloneo
Created January 19, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diabloneo/130317e801984fa0953f to your computer and use it in GitHub Desktop.
Save diabloneo/130317e801984fa0953f to your computer and use it in GitHub Desktop.
Execute command with timeout
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