Sometimes it’s desirable to have a chain of commands backgrounded so that a multi-step process can be run in parallel. And often times its not desirable to have yet another script made to do a simple task that doesn’t warrant the added complexity. An example of this would be running backups in parallel. The script sniplet below would allow up to 4 simultaneous tar backups to run at once — recording the start and stop times of each individually — and then wait for all the tar processes to finish before exiting
max_tar_count=4 for i in 1 3 5 7 2 4 6 8 do cur_tar_count=$(ps wauxxx | grep -v grep | grep tar | wc -l) if [ $cur_tar_count -ge $max_tar_count ] then while [ $cur_tar_count -ge $max_tar_count ] do sleep 60 cur_tar_count=$(ps wauxxx | grep -v grep | grep tar | wc -l) done fi ( echo date > /backups/$i.start && tar /backups/$i.tar /data/$i && echo date > /backups/$i.stop )& done cur_tar_count=$(ps wauxxx | grep -v grep | grep tar | wc -l) while [ $cur_tar_count -gt 0 ] do sleep 60 cur_tar_count=$(ps wauxxx | grep -v grep | grep tar | wc -l) done
The real magick above is highlighted in red. You DO want that last loop in there to make the script wait until all the backups are really done before exiting.