Created
October 24, 2019 11:25
-
-
Save nordicdyno/b2169405c6dafd41c8d64ba0adfb96ef to your computer and use it in GitHub Desktop.
одновременный запуск некоторого количества процессов в баше и потом нужно остановиться если хоть один умер и показать stdout
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
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