Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continuously reading from exec.Cmd output

Tags:

stdout

go

exec

Guys I am trying pick new lines as they come from command output, but always I end up doing it synchronous way (I have to wait until script is finished). I tired to use fsnotify but it is working only with regular files, do you have any idea how it can be done ?

package main
import (
   "fmt"
   "os/exec"
   "bytes"
   "os"
)

func main() {
   cmd := exec.Command("scripts/long_script")
   output := new(bytes.Buffer)
   cmd.Stdout = output
   cmd.Stderr = output
   if err := cmd.Start(); err != nil{ // after Start program is continued and script is executing in background
     fmt.Printf("Failed to start " + err.Error())
     os.Exit(1)
   }
   fmt.Printf(" Before WAIT %s \n", output.String())  // script is writing but nothing can be read from output
   cmd.Wait()
   fmt.Printf(" After Wait %s \n", output.String())  // if we wait to finish execution, we can read all output
}
like image 796
ugi Avatar asked Apr 26 '17 13:04

ugi


2 Answers

You should use os.StdoutPipe()

func main() {
    for i := 10; i < 20; i++ {
        go printName(`My name is Bob, I am ` + strconv.Itoa(i) + ` years old`)
        // Adding delay so as to see incremental output
        time.Sleep(60 * time.Millisecond)
    }
    // Adding delay so as to let program complete
    // Please use channels or wait groups
    time.Sleep(100 * time.Millisecond)
}

func printName(jString string) {
    cmd := exec.Command("echo", "-n", jString)
    cmdReader, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatal(err)
    }
    scanner := bufio.NewScanner(cmdReader)
    go func() {
        for scanner.Scan() {
            fmt.Println(scanner.Text())
        }
    }()
    if err := cmd.Start(); err != nil {
        log.Fatal(err)
    }
    if err := cmd.Wait(); err != nil {
        log.Fatal(err)
    }
}

sources that helped me: nathanleclaire.com blog.kowalczyk.info

like image 101
Gaurav Raghuvanshy Avatar answered Sep 24 '22 17:09

Gaurav Raghuvanshy


eventually I managed to do it with []bytes

stdout, err := cmd.StdoutPipe()
buff := make([]byte,10)
var n int
for err == nil {
    n,err = stdout.Read(buff)
    if n > 0{
        fmt.Printf("taken %d chars %s",n,string(buff[:n]))
    }
}
cmd.Wait()
if cmd.ProcessState.Success() {. // ProcessState is set after Wait
    fmt.Println("Script success")  
} else {
    fmt.Println("Script failed")
}
like image 27
ugi Avatar answered Sep 21 '22 17:09

ugi