Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to program wait and continue in this bash script

I have two shell scripts say A and B. I need to run A in the background and run B in the foreground till A finishes its execution in the background. I need to repeat this process for couple of runs, hence once A finishes, I need to suspend current iteration and move to next iteration.

Rough idea is like this:

for((i=0; i< 10; i++))  
do  
./A.sh &

for ((c=0; c< C_MAX; c++))  
do  
./B.sh  
done

continue

done

how do I use 'wait' and 'continue' so that B runs as many times while A is in the background and the entire process moves to next iteration once A finishes

like image 211
marc Avatar asked Feb 15 '26 23:02

marc


1 Answers

Use the PID of the current background process:

./A.sh &
while ps -p $! >/dev/null; do
    ./B.sh
done
like image 70
thiton Avatar answered Feb 19 '26 06:02

thiton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!