본문 바로가기
software engineering/shell

서버 기동 후 간단한 health checker function

by _블로그 2021. 1. 8.
#!/bin/bash

WAIT_TIME=60
CHECK_API="/check"

# 10초 기다렸다가 check api를 WAIT_TIME 내에서 초마다 체크
check_server() {
  local t=0
  while [ ${t} -lt 10 ]; do
    sleep 1
    echo -n "."
    ((t++))
  done
  t=0
  while [ ${t} -lt ${WAIT_TIME} ]; do
    sleep 1
    echo -n "."
    local res_code
    res_code=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost${CHECK_API}")
    if [ ${res_code} -eq 200 ]; then
      break
    fi
    if [ $? -eq 0 ]; then
      break
    fi
    ((t++))
  done

  if [ ${t} -eq ${WAIT_TIME} ]; then
    printf "\n\nSomething's wrong. Check server status.\n"
    exit 1
  fi
}

printf "# startup server\n"
###
# here is for startup server.
###

check_server

if [ $? -ne 0 ]; then
  printf "error.\n"
  exit 1
fi

printf "\ndone.\n"
LIST

댓글