Skip to content

Instantly share code, notes, and snippets.

@comuttun
Created September 6, 2012 07:20
Show Gist options
  • Select an option

  • Save comuttun/3652522 to your computer and use it in GitHub Desktop.

Select an option

Save comuttun/3652522 to your computer and use it in GitHub Desktop.
Startup checker
#!/bin/bash
. /etc/init.d/functions
LOGFILE=/tmp/tmp.log
KEYWORD=Started
CHECK_INTERVAL=5
TIMEOUT=30
TIME_COMMAND='date +%s'
if [ ! -f ${LOGFILE} ]; then
echo "ERROR: ${LOGFILE} does not exist" >&2
exit 1
fi
tail -0f ${LOGFILE} 2>/dev/null | grep -m1 ${KEYWORD} >/dev/null 2>&1 &
TAIL_PID=$(pgrep -P $$ tail)
GREP_PID=$(pgrep -P $$ grep)
function kill_process {
declare -i pid=$1
declare name=$2
if ! ps -p ${pid} >/dev/null 2>&1; then
return 0
fi
kill -KILL ${pid} >/dev/null 2>&1
if ! ps -p ${pid} >/dev/null 2>&1; then
failure
echo
echo "ERROR: killing ${name} (pid=${pid}) faield" >&2
return 1
fi
return 0
}
function kill_grep {
kill_process ${GREP_PID} grep
return $?
}
function kill_tail {
kill_process ${TAIL_PID} tail
return $?
}
echo "Waiting for ${LOGFILE}... "
START_TIME=$(${TIME_COMMAND})
while :; do
CURRENT_TIME=$(${TIME_COMMAND})
# grep が終了しているかどうかをチェック
if ! ps -p ${GREP_PID} >/dev/null 2>&1; then
kill_tail
if [ $? -eq 0 ]; then
success
echo
fi
break
else
TIME_ELAPSED=$((${CURRENT_TIME} - ${START_TIME}))
if [ ${TIME_ELAPSED} -ge ${TIMEOUT} ]; then
kill_grep
kill_tail
echo "ERROR: Timed out" >&2
break
fi
fi
sleep ${CHECK_INTERVAL}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment