2017-11-10 11 views
0

私は2つのスクリプトを持っています。第二は第一に依存する。2番目のスクリプトのバックグラウンド・パラレル・プロセスが終了するまで、2番目のスクリプトがトリガーするのを待ちますか?

#!/bin/sh <br> 
# This is First Script! 
echo script 1 start here  # This is a comment, too! 

for number in 1 2 3 4 5 
do 
echo $number & # Here five backgroud process triggers 
done 

echo Script one ends here 

#!/bin/sh 
# This is Second Script! 
echo Script 2 starts </t># This is a comment, too! 

for number in 1 2 3 4 5 
do 
echo $number & # here other five background process triggers 
done 
echo Script 2 ends here 

答えて

2

:秒完了せずに起動するように、しかし、最初のスクリプトは、以下のサンプルコードを追加してい最初のスクリプトcompletes.Iのすべてのバックグラウンド・プロセスまで、2番目のスクリプトを待つか、停止するためにとにかくまずScript.Isのすべてのバックグラウンド・プロセスをバックグラウンド・プロセスを持っています$を使用してください!バックグラウンドプロセスのpidを記録し、それを使用してセカンダリスクリプトの実行ポイントを決定します。

元のスクリプトリストsh、waitはbashの関数です。可能であれば、bashを使用してください。 機能は例えば、SH

に存在します。

#!/bin/bash 
echo "I'm Starting Now" 
sleep 60 & #this process now executes in the bg 
bg_pid=$! 
wait $bg_pid #this will pause the script until pid is dead. 
echo "You will not see this until sleep is complete" 

あなたはそれが次のようになります掲載スクリプトにこれを組み込むには:ループについて

#!/bin/bash 
# This is First Script! 
echo 'script 1 start here'  # This is a comment, too! 

for number in 1 2 3 4 5 
do 
    echo $number & # Here five background process triggers 
    declare bgpid[$number]=$! #we will pack all the pids into an array 
done 

wait ${bgpid[@]} #this exposes the array to wait 
echo 'Script one ends here' 
#Call next script 
+0

何?また$!最後に実行中のpidを保存しますか? –

+0

はい、$!最後に実行されたコマンドのpidを返します。複数のアイテムをbgプロセスとして渡していて、非同期で終了する場合は、各pidを渡す必要があります。 waitは複数の引数を受け入れることができます。 –

+0

これが助けられたり、そうでなければ正しかったなら、私はupvoteと答えのタグに感謝します:)。 –

関連する問題