Reputation: 529
I would like to run multiple commands in parallel in a bash script, but if any of these commands fails (returns a non-zero exit code), the script exit code must be non-zero.
I have tried to use sem
(https://www.gnu.org/software/parallel/sem.html):
cat >script.sh <<EOF
sem -j+0 "sleep 2; echo 1"
sem -j+0 "sleep 4; exit 1; echo 2"
sem -j+0 "sleep 6; echo 3"
sem --wait
EOF
bash script.sh; echo $?
and just background the process:
cat >script.sh <<EOF
{sleep 2; echo 1} &
{sleep 4; exit 1; echo 2} &
{sleep 6; echo 3} &
wait
EOF
bash script.sh; echo $?
In both cases, the overall exit code is always 0.
Any ideas?
Upvotes: 5
Views: 2014
Reputation: 529
Thanks everybody for their answers.
Following Mark Setchell's suggestion, I think the best solution for me is:
#!/bin/bash
set -euo pipefail
cat <<EOF | parallel --halt 1
date; sleep 2; date; echo -e "1\n"
date; sleep 4; exit 1; date; echo -e "2\n"
date; sleep 6; date; echo -e "3\n"
EOF
Upvotes: 2
Reputation: 141463
but if any of these commands fails (returns a non-zero exit code), the script exit code must be non-zero.
So write that condition.
childs=();
{ sleep 0.2; echo 1 ;} &
childs+=($!);
{ sleep 0.4; exit 1; echo 2; } &
childs+=($!);
{ sleep 0.6; echo 3; } &
childs+=($!);
ret=0;
for i in "${childs[@]}"; do
if ! wait "$i"; then ret=1; fi
done
echo "ret=$ret"
exit "$ret"
Upvotes: 4