Last active
March 2, 2025 06:36
-
-
Save bamoo456/7e21773e8ef742a726c041f5f0019d2e to your computer and use it in GitHub Desktop.
[Golang] execute long running job and streaming the realtime output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
thx for this
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?
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
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
Any idea on how to do this in Windows? Windows flushes all the logs at once in the end