Skip to content

Instantly share code, notes, and snippets.

@luqmansen
Created June 3, 2021 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqmansen/ade20dc6425581bd711529f44761cbf4 to your computer and use it in GitHub Desktop.
Save luqmansen/ade20dc6425581bd711529f44761cbf4 to your computer and use it in GitHub Desktop.
stream stdout from long running asynchronous cmd
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