Need help in a bash script. The goal is: - Run several commands in parallel - Exit 1 if any command return not-zero exit status
I.e.
Run with middle command has error:
$ ./parallel_commands "echo 1" "_echo 2" "echo 3" && echo "OK"
1
3
./parallel_commands: line 4: _echo: command not found
OK <- Incorrect
Run with all commands have errors:
$ ./parallel_commands "_echo 1" "_echo 2" "_echo 3" && echo "OK"
./parallel_commands: line 4: _echo: command not found
./parallel_commands: line 4: _echo: command not found
./parallel_commands: line 4: _echo: command not found
-> Result is fail -> Correct
Bash script:
#!/bin/bash
for cmd in "$@"; do {
$cmd & pid=$!
PID_LIST+=" $pid";
} done
trap "kill $PID_LIST" SIGINT
wait $PID_LIST
Thanks.