you want threads in bash and know exactly when they finish and in which state?
#!/bin/bash
threads=()
f_getThreadKeyword(){ for i in ${threads[@]}; do [[ $(echo $i|cut -d':' -f2) == $1 ]] && echo $(echo $i|cut -d':' -f1); done; } # returns the keyword for a given pid
f_addThread(){ keyword=$1; pid=$2; threads+=( "$keyword:$pid" ); echo "added thread $keyword ($pid)"; } # adds a thread to the array
f_removeThread(){ pid=$1; threads=( ${threads[@]/$(f_getThreadKeyword $pid):$pid} ); } # removes a thread to the array
f_getThreadPids(){ echo ${threads[@]}|tr ' ' '\n'|cut -d':' -f2|tr '\n' ' '; } # returns all pids
sleep 3 &
f_addThread first $!
sleep 10 &
f_addThread second $!
sleep 5 &
f_addThread third $!
while [[ $(jobs|grep -iE 'läuft|running' -c) -gt 0 ]]; do # there are running background jobs
wait -p next -n $(f_getThreadPids)
err=$?
echo pid $next exited with $err \($(f_getThreadKeyword $next)\)
f_removeThread $next
done
echo done
Output:
added thread first (1429283)
added thread second (1429285)
added thread third (1429286)
pid 1429283 exited with 0 (first)
pid 1429285 exited with 143 (second)
pid 1429286 exited with 0 (third)
(I killed the second thread from another console)