Last active
August 29, 2015 14:02
-
-
Save tkuchiki/0f3a4ef059b559129f5e to your computer and use it in GitHub Desktop.
daemontools で service を起動できていなかったら、service を停止して symlink を消す
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 | |
#!/bin/bash | |
TARGET="${1}" | |
LINKNAME="${2}" | |
IS_DELETED=${3:-0} | |
SERVICE="$(basename $LINKNAME)" | |
declare -i SEC=0 | |
declare -i COUNT=0 | |
if ps -ef | grep -v grep | grep -v "${0}" | grep -qs "supervise ${SERVICE}$"; then | |
echo "${SERVICE} is already running" | |
exit 0 | |
else | |
if [ -d "${LINKNAME}" ]; then | |
LINKNAME="${LINKNAME}/$(basename $TARGET)" | |
fi | |
if [ -L "${LINKNAME}" ]; then | |
echo "ln: creating symbolic link '${LINKNAME}': Symbolic link exists" | |
exit 0 | |
elif [ -f "${LINKNAME}" ]; then | |
echo "ln: creating symbolic link '${LINKNAME}': File exists" | |
exit 1 | |
fi | |
ln -s "${TARGET}" "${LINKNAME}" | |
while :; do | |
STR_SEC=$(svstat $LINKNAME | grep seconds | cut -d ' ' -f 5) | |
SEC="${STR_SEC}" | |
sleep 1 | |
if [ $COUNT -gt 10 ]; then | |
# fail | |
declare -i PID=0 | |
declare -i __SEC=0 | |
READLINK=$(readlink -f $LINKNAME) | |
for DIR in $(find -L $READLINK -type d | grep -v ${LINKNAME}$); do | |
echo $(readlink -f $DIR) | grep -qs ^$READLINK || continue | |
PID_SEC=$(svstat $DIR | cut -d ' ' -f 4,5 | sed -e 's/)//' | sed -e 's|[/:,]||g') | |
PID=$(echo $PID_SEC | cut -d ' ' -f 1) | |
__SEC=$(echo $PID_SEC | cut -d ' ' -f 2) | |
[ $PID = 0 -a $__SEC = 0 ] && continue | |
if [ $__SEC -lt 3 ]; then | |
svc -dx $DIR | |
elif ps -ef | grep $PID | grep -qs multilog; then | |
svc -dx $DIR | |
fi | |
done | |
svc -dx "${LINKNAME}" | |
rm -f "${LINKNAME}" | |
echo "${LINKNAME} does not start" | |
exit 1 | |
elif [ $SEC -gt 3 ]; then | |
# success | |
[ $IS_DELETED != 0 ] && rm -f "${LINKNAME}" | |
break | |
else | |
COUNT="${COUNT} + 1" | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment