スクリプトで並列に開始されたプロセスの終了コードを確認する

スクリプトで並列に開始されたプロセスの終了コードを確認する

次のbashスクリプトがあるとしましょう。

#!/usr/bin/env bash
process-1&
process-2&
process-3&

2つの質問があります。

  1. すべてのプロセスが終了コード0で終了したことをスクリプトの最後でどのように確認しますか?
  2. 開始されたすべてのプロセスが完了するまでスクリプトを「待機」してから要約できますecho(ユーザーに終了コードを知らせることができます)。

ベストアンサー1

bash 組み込みを使用できますwait
3回wait -n、毎回終了状態を確認します。

~からman bash:

    wait [-n] [n ...]
           Wait  for  each  specified child process and return its termination
           status.  Each n may be a process ID or a job  specification;  if  a
           job  spec is given, all processes in that job's pipeline are waited
           for.  If n is not given, all currently active child  processes  are
           waited  for,  and  the  return status is zero.  If the -n option is
           supplied, wait waits for any job to terminate and returns its  exit
           status.   If  n specifies a non-existent process or job, the return
           status is 127.  Otherwise, the return status is the exit status  of
           the last process or job waited for.

おすすめ記事