0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

set -e中のwaitにはpidを1つ書こう

Posted at

シェルスクリプトを書いていたところ、waitの思わぬ仕様に面食らってしまいました。

問題の発覚

Webアプリケーションのデプロイ処理で、多数のコマンドを呼び出す必要があったので、シェルスクリプトを書いてそれらをつなぎ合わせていました。そして、デプロイ処理を行うマシンのスペックに余裕があったので、並列でコマンドを実行させるようにしました。

#!/bin/sh

set -e

some_container_build_command &
some_database_seed_command &
wait
some_javascript_bundle_command
some_container_push_command

ところがある日のこと、DBのシードに失敗したにもかかわらず、set -eでスクリプト全体が失敗することもないままコンテナのプッシュまで進行してしまったことが判明しました。

何が起きた?

調べてみると、waitの挙動は以下のようなものでした(POSIX.1-2017)。

wait(引数なし)→ 自身は0で終了する
wait [1個以上のpid...]wait自体の終了コードは、最後に与えたpidのプロセスの終了コード(仮に最後のpidが適切でなかった場合は127)

対策法

ということで、問答無用に成功するwaitオンリーはいうまでもなく、最後の1つの失敗しか検知できない複数表記というのも微妙なので、原始的にwait pidを個数だけ連ねて対応することにしました。

#!/bin/sh

set -e

some_container_build_command &
container_build_pid=$!
some_database_seed_command &
seed_pid=$!
wait $container_build_pid
wait $seed_pid
some_javascript_bundle_command
some_container_push_command

終わりに

日常的に使っているコマンドにすら、想定しないような仕様があることも珍しくない。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?