I am trying to create a script which will start many background command. For each background command I need to get the return code.
I have been trying the following script :
#!/bin/bash
set -x
pid=()
return=()
for i in 1 2
do
echo start $i
ssh mysql "/root/test$i.sh" &
pid[$i]=$!
done
for i in ${#pid[@]}
do
echo ${pid[$i]}
wait ${pid[$i]}
return[$i]=$?
if [ ${return[$i]} -ne 0 ]
then
echo mail error
fi
done
echo ${return[1]}
echo ${return[2]}
My issue is during the wait loop, if the second pid finish before the first one, I'll not be able to get the return code.
I know that I can run wait pid1 pid2, but with this command I can't get the return code of all commands.
Any idea ?