LoginSignup
29

More than 5 years have passed since last update.

複数のバックグラウンドプロセスが全て成功したか判別する方法

Last updated at Posted at 2016-06-28

環境

bash4.3

スクリプト

#!/bin/sh -x
pids=()
# ok.sh は 0 を返す
# ng.sh は 1 を返す
commands=("./ok.sh" "./ng.sh")

for command in ${commands[@]}; do
  ${command} &
  pids+=($!)
done

for pid in ${pids[@]}; do
  wait $pid
  if [ $? -ne 0 ]; then
    exit 1
  fi
done

ポイント

  • $! : 最後に起動したバックグラウンドプロセスのPID
  • wait ${pid} で、対象のpidのプロセスが終了するまで待機
  • wait の戻り値は、対象のプロセスの戻り値

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29