バックグラウンドで複数のプログラムを実行し、戻り値を確認してください。

バックグラウンドで複数のプログラムを実行し、戻り値を確認してください。

次のように複数(たとえば3つ)のプログラムを同時に実行したいとしましょう。

program1 & program2 & program3 &

知りたいなら、pid'sこんなに保管しておくのが最善の方法だと思います。

program1 &
pid1=$!
program2 &
pid2=$!
...

しかし、今終了したかどうかを知りたい場合は、どの値を返しますか?このような:

if [ program1 has ended ]; then
    do something with its return value
fi

どんな助けでも大変感謝します。

ベストアンサー1

# launch child process in background
launch_child_process &

# grab child's pid and store it (use array if several)
pid=$!

# later, wait for termination
wait $pid
# show return code from wait, which is actually the return code of the child
echo "Child return code:" $?

複数の子がある場合は、PIDごとにループを待って対応する戻りコードを収集できます。最後のサブプロセスが終了したら、ループを終了できます。

おすすめ記事