Skip to content

Instantly share code, notes, and snippets.

@bamoo456
Last active August 7, 2024 16:23
Show Gist options
  • Save bamoo456/7e21773e8ef742a726c041f5f0019d2e to your computer and use it in GitHub Desktop.
Save bamoo456/7e21773e8ef742a726c041f5f0019d2e to your computer and use it in GitHub Desktop.
[Golang] execute long running job and streaming the realtime output
func main() {
cmd := exec.Command("sh", "-c", "cd ../../bin/worker; ./run.sh")
// some command output will be input into stderr
// e.g.
// cmd := exec.Command("../../bin/master_build")
// stderr, err := cmd.StderrPipe()
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
err = cmd.Start()
fmt.Println("The command is running")
if err != nil {
fmt.Println(err)
}
// print the output of the subprocess
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
cmd.Wait()
}
@pandulaDW
Copy link

Any idea on how to do this in Windows? Windows flushes all the logs at once in the end

@bbq2100
Copy link

bbq2100 commented Dec 16, 2020

thx for this

@SirLich
Copy link

SirLich commented Aug 2, 2021

Any idea on how to do this in Windows? Windows flushes all the logs at once in the end

Did you ever figure this out?

@jwomackgsa
Copy link

Not sure if anyone is still looking for a solution, but I found that this project seems to work well as a wrapper for os/exec that supports streaming output from a go routine.
https://github.com/go-cmd/cmd

@jbeguin
Copy link

jbeguin commented Jun 22, 2023

nobody search a thing like that :) not tested but thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment