Tutorial by Examples

c := exec.Command(name, arg...) b := &bytes.Buffer{} c.Stdout = b c.Stdin = stdin if err := c.Start(); err != nil { return nil, err } timedOut := false intTimer := time.AfterFunc(timeout, func() { log.Printf("Process taking too long. Interrupting: %s %s", name, strings...
// Execute a command a capture standard out. exec.Command creates the command // and then the chained Output method gets standard out. Use CombinedOutput() // if you want both standard out and standerr output out, err := exec.Command("echo", "foo").Output() if err != nil { ...
cmd := exec.Command("sleep", "5") // Does not wait for command to complete before returning err := cmd.Start() if err != nil { log.Fatal(err) } // Wait for cmd to Return err = cmd.Wait() log.Printf("Command finished with error: %v", err)
A Cmd cannot be reused after calling its Run, Output or CombinedOutput methods Running a command twice will not work: cmd := exec.Command("xte", "key XF86AudioPlay") _ := cmd.Run() // Play audio key press // .. do something else err := cmd.Run() // Pause audio key press,...

Page 1 of 1