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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nobody search a thing like that :) not tested but thx.