Skip to content

Instantly share code, notes, and snippets.

@nordicdyno
Created October 24, 2019 11:25
Show Gist options
  • Save nordicdyno/b2169405c6dafd41c8d64ba0adfb96ef to your computer and use it in GitHub Desktop.
Save nordicdyno/b2169405c6dafd41c8d64ba0adfb96ef to your computer and use it in GitHub Desktop.
одновременный запуск некоторого количества процессов в баше и потом нужно остановиться если хоть один умер и показать stdout
package main
import (
"fmt"
"log"
"os"
"os/exec"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
cmd := exec.Command("echo", fmt.Sprintf("Hello %v", i))
if i == 0 {
cmd = exec.Command("bash", "-c", "sleep 1 && echo Dying && exit 1")
}
wg.Add(1)
go func(cmd *exec.Cmd) {
defer wg.Done()
out, err := cmd.CombinedOutput()
checkCmd(cmd, err, out)
}(cmd)
}
wg.Wait()
}
func checkCmd(cmd *exec.Cmd, err error, b []byte) {
if err == nil {
return
}
log.Printf("Command \"%v\" exit with error: %v\n", cmd.String(), err)
fmt.Println("OUTPUT:")
fmt.Println(string(b))
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment