Created
January 25, 2012 05:37
-
-
Save DQNEO/1674945 to your computer and use it in GitHub Desktop.
重複起動防止付きシェルスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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