2

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

4

2 回答 2

3

使用当前后台进程的PID:

./A.sh &
while ps -p $! >/dev/null; do
    ./B.sh
done
于 2011-11-04T19:15:48.613 回答
1

我只是将您的粗略想法翻译成 bash 脚本。实现等待继续机制while ps -p $A_PID >/dev/null; do...

for i in `seq 0 10`
do
  ./A.sh &
  A_PID=$!
  for i in `seq 0 $C_MAX`
  do
    ./B.sh
  done
  while ps -p $A_PID >/dev/null; do
      sleep 1
  done
done
于 2011-11-04T20:26:04.930 回答