7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashでバックグラウンドプロセスの終了を待つ

Last updated at Posted at 2017-05-30

ちゃんと停止コマンドがあるやつはいいけど killで停止するやつとかはちゃんと停止を待ってあげる必要がある。
最近、docker-entrypoint.shでよく書いてるのでメモする。

#!/bin/bash
set -e

## 
## なにがしかの処理
##

# &使ってバックグラウンドプロセスにしてる場合(実行の次の行に書く)
# nohup hogehoge &
# pid=$!

stop() {
    echo " * Stopping"
    # pidファイルを作ってた場合
    # local pid=`cat xxx.pid`

    kill $pid
    local psalive=`kill -0 $pid > /dev/null 2>&1; echo $?`
    while [ "$psalive" = "0" ]
    do
        sleep 1
        psalive=`kill -0 $pid > /dev/null 2>&1; echo $?`
    done
    echo "   ...done."
}


# SIGTERMで引っ掛けたい場合
# trap 'stop' TERM
7
10
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
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?