2013-10-29 17 views

回答

1

當我做類似的事情,我保存的進程id的數組中的:

#!/bin/bash 

declare -a pids 

idx=0 
while test $idx -lt 4; do 
    ./run.sh & 
    pids[$idx]=$! 
    idx=$(($idx+1)) 
done 

你也可以設置其他的陣列,以節省啓動時間或無論您需要跟蹤哪些其他信息。然後設置另一個循環來檢查已完成的進程。這個循環會根據退出條件而改變。

done=0 
while test $done -lt 4; do 
    idx=0 
    while test $idx -lt 4; do 
    pid=${pids[$idx]} 
    if [[ $pid -ne 0 ]]; then 
     kill -0 $pid 
     if [[ $rc -ne 0 ]]; then 
     $pids[$idx]=0 # completed. 
     done=$((done+1) 
     fi 
    fi 
    done 
    sleep 1 
done 
+0

此代碼需要數組支持,它可能在所有shell中都不可用(該問題僅標記爲「shell」)。 – chepner

1

你可以試試:

#main.sh 
time ./run1.sh & 
time ./run2.sh & 
time ./run3.sh & 
... 
time ./run8.sh & 
wait 
echo "Done" 
相關問題