Go get CMD output in real time

809 views
Skip to first unread message

Paulo Coutinho

unread,
May 13, 2016, 1:43:33 AM5/13/16
to golang-nuts
Hi

In my projecto on github.com/prsolucoes/goci im executing jobs in JavaScript throught otto parser.

When i need execute something using CMD i only get the output after finish. I need get while i receive to store on job output.

Hoe can i do that?

Thanks.

Dave Cheney

unread,
May 13, 2016, 1:55:22 AM5/13/16
to golang-nuts

Paulo Coutinho

unread,
May 13, 2016, 1:47:41 PM5/13/16
to golang-nuts
I already read about it, but i dont understand if it can help me.

I think that exists something that i can pass a closure and add the newlines to job output variable.

ex:

cmd.OnOutput(func (output string)) {
     job.Log(output)
     or
     job.OutputGroup[0] += output
}

Thanks.

Shawn Milochik

unread,
May 13, 2016, 4:08:15 PM5/13/16
to golang-nuts
You can do it like this:

 $ cat count.sh 
#!/usr/bin/env bash

for x in {1..3}; do
    echo $x
    sleep 1
done

 $ cat main.go 
package main

import (
"os"
"os/exec"
)

func main() {
cmd := exec.Command("./count.sh")
cmd.Stdout = os.Stdout
cmd.Run()
}

Paulo Coutinho

unread,
May 13, 2016, 9:07:31 PM5/13/16
to golang-nuts, Sh...@milochik.com
Hi,

I solved the problem. Anyone can check here:


@Dave, you are correct.

Thanks for any help.
Reply all
Reply to author
Forward
0 new messages