Skip to content

Instantly share code, notes, and snippets.

@DQNEO
Created January 25, 2012 05:37
Show Gist options
  • Save DQNEO/1674945 to your computer and use it in GitHub Desktop.
Save DQNEO/1674945 to your computer and use it in GitHub Desktop.
重複起動防止付きシェルスクリプト
#!/bin/bash
##
# 重複起動防止
# /tmp/{スクリプト名}.pid というファイルが作成されます。
#
# テスト方法
# while true ; do script.sh ; done
function pid_start() {
local BASE_NAME=$(basename $0)
PID_FILE="/tmp/$BASE_NAME.pid"
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
if (ps -e | awk '{print $1}' | grep $PID >/dev/null); then
echo "This process is running"
exit 1
fi
fi
echo $$ > $PID_FILE
}
function pid_end() {
rm $PID_FILE
}
pid_start
# ここでメインの処理
pid_end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment