Skip to content

Instantly share code, notes, and snippets.

@udondan
Last active May 3, 2018 07:45
Show Gist options
  • Save udondan/00c3b48eada0a38349643f584a85ee38 to your computer and use it in GitHub Desktop.
Save udondan/00c3b48eada0a38349643f584a85ee38 to your computer and use it in GitHub Desktop.
os/exec CommandContext example
package main
import (
"context"
"os"
"os/exec"
"time"
)
func main() {
timeout := 3 * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
cmd := exec.CommandContext(ctx, "sleep", "10")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
panic(err)
}
if err := cmd.Wait(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment