Testing a process has ended (in the background)

Hi guys. Hopefully this question will make sense!

Continuing on my script to automatically copy some huge files across the network onto various servers as background jobs, I need to be able to check that each job has finished successfully.

The script below shows what I want - almost. The problem is that some processes may obviously finish sooner than others, even though they were started afterwards. But the way the script is written it will do the first one, and when that's completed check the second one (which may already be done).

What I need is a way of getting the wait and exit code parts to also run as background jobs so they will finish as soon as the process has - but I'll be darned if I can work out how that would be written. I'm sure it'll be simple, but my brain is fried!

Anyone got any ideas? Thanks in advance. :b:

set -A processvar
countervar=1
while [[ $countervar -le 4 ]]
do
   seconds=$(( (RANDOM%60+1) ))
   echo $seconds
   sleep $seconds &
   processvar[$countervar]=$!
   (( countervar=countervar+1 ))
done

countervar=1
while [[ $countervar -le 4 ]]
do
   wait ${processvar[$countervar]} ; echo Exitstate = $?
   (( countervar=countervar+1 ))
done

are you using rsync to copy the large files? or scp? or something else?

Hi, I'm using SCP to do the copy. Sorry, I should have mentioned but I am using "sleep" in the example script just to show the situation. In the real one sleep will be replaced with an scp to copy the files.