Created
June 3, 2021 14:56
-
-
Save luqmansen/ade20dc6425581bd711529f44761cbf4 to your computer and use it in GitHub Desktop.
stream stdout from long running asynchronous cmd
This file contains 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os/exec" | |
"time" | |
) | |
func main() { | |
cmd := exec.Command("bash", "-c", "while true; do echo \"yeet\" && sleep 1;done") | |
pipe, _ := cmd.StdoutPipe() | |
if err := cmd.Start(); err != nil { | |
// handle error | |
} | |
go func() { | |
for { | |
reader := bufio.NewReader(pipe) | |
line, err := reader.ReadString('\n') | |
for err == nil { | |
fmt.Println(line) | |
line, err = reader.ReadString('\n') | |
} | |
} | |
}() | |
for { | |
fmt.Println("other command") | |
time.Sleep(2 * time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment