go - Golang pipe output of subcommand real time -
i trying pipe output of command, no data seems read pipe until write end closed. want connect websocket streams status of command while being executed. problem while code prints messages line line, not print until program has finished executing.
cmd := exec.command(my_script_location, args) // create pipe output of script // todo pipe stderr cmdreader, err := cmd.stdoutpipe() if err != nil { fmt.fprintln(os.stderr, "error creating stdoutpipe cmd", err) return } scanner := bufio.newscanner(cmdreader) go func() { scanner.scan() { fmt.printf("\t > %s\n", scanner.text()) } }() err = cmd.start() if err != nil { fmt.fprintln(os.stderr, "error starting cmd", err) return } err = cmd.wait() if err != nil { fmt.fprintln(os.stderr, "error waiting cmd", err) return }
is there way can similar , have scanner read line line written pipe instead of after has been written? program takes 20 seconds run, , there steady stream of updates, annoying have them go through @ once.
turns out issue not in code posted above. works expected. problem c program being executed not flushing stdout
. when running interactively, worked expected, when stdout
piped, not written until called flush
. after manually adding flush statements c program, go code worked expected.
Comments
Post a Comment