1.非同期実行(バックグラウンド実行)
コマンドの後に”&”を付与する。
2.非同期実行時のプロセスIDを取得する
"$!"で取得する。
3. 実行後の結果(リターンコード)取得
非同期実行を行った処理の終了を待った後に、"$?"で取得。
wait プロセスID
$?
#exec.sh
#!/usr/bin/sh
sh test.sh aa &
test1pid=$!
sh test.sh bb &
test2pid=$!
sh test.sh cc &
test3pid=$!
wait $test1pid
echo $?
wait $test2pid
echo $?
wait $test3pid
echo $?
echo end
exit 0
#test.sh
#!/usr/bin/sh
a=1
while [ $a -lt 1000 ]
do
if [ $a -eq 250 ]; then
echo "a = $1: $a"
elif [ $a -eq 500 ]; then
echo "a = $1: $a"
elif [ $a -eq 750 ]; then
echo "a = $1: $a"
fi
a=`expr $a + 1`
done
if [ $1 = "aa" ]; then
exit 1
elif [ $1 = "bb" ]; then
exit 2
elif [ $1 = "cc" ]; then
exit 3
fi
PR