LoginSignup
16
17

More than 5 years have passed since last update.

シェルスクリプトの二重起動抑止(起動コマンドライン完全一致時)

Posted at

pgrepコマンドやlockファイルを使用した抑止については一般的にいくつかありますが、なかなかシンプルに完全な二重起動抑止をするのは難しいようです。
色々と検討中ではありますが、その中で(恐らく)限定状況下においては効果を発揮しそうな形の抑止処理がありましたので記述します。

・起動コマンドラインが引数を含めて完全一致する場合
 ※cron起動で引数が固定である場合などで有効か?

Bash
# 起動プロセスの完全名を取得
# cmdlineはnull区切りなのでxargsをかませる
CMDLINE=$(cat /proc/$$/cmdline | xargs --null)

# 名称が完全に一致しているプロセスが他に無ければOK
if [[ $(pgrep -oxf "${CMDLINE}") -ne $$ ]];then
    echo "error message."
    exit 1
fi
16
17
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
16
17