Skip to content

Instantly share code, notes, and snippets.

@bamoo456
Last active March 2, 2025 06:36
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()
}
@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