Skip to content

Instantly share code, notes, and snippets.

@audstanley
Last active July 28, 2021 21:04
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 audstanley/417c7e18616664209f86c9bab91da729 to your computer and use it in GitHub Desktop.
Save audstanley/417c7e18616664209f86c9bab91da729 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
"time"
)
func runThing(c1 chan string, wg *sync.WaitGroup) {
defer wg.Done()
cmd := exec.Command("./subprocess.sh")
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if nil != err {
log.Fatalf("Error obtaining stdout: %s", err.Error())
}
reader := bufio.NewReader(stdout)
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
log.Printf("running subprocess: %s", scanner.Text())
c1 <- scanner.Text()
}
}(reader)
if err := cmd.Start(); nil != err {
log.Fatalf("Error starting program: %s, %s", cmd.Path, err.Error())
}
fmt.Println("we are near the end")
cmd.Wait()
fmt.Println("we are done")
}
func channelListener(c1 <- chan string) {
for {
select {
case msg := <-c1: {
fmt.Println("message:", msg)
}
default: {
time.Sleep(time.Millisecond*100)
}
}
}
}
func main() {
var wg sync.WaitGroup
wg.Add(2)
c1 := make(chan string)
go runThing(c1, &wg)
go runThing(c1, &wg)
go channelListener(c1)
wg.Wait()
}
#!/bin/bash
echo "blah blah blah"
sleep 10
echo "sub done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment