http 서버 모니터링

http 서버 모니터링

웹 서버 상태를 확인하고 싶습니다. 2분 안에 http-응답을 받았다면 서버가 다시 시작되어야 합니다. 상태를 확인하기 위해 아래 명령을 사용하고 있습니다.

curl -s -w "%{time_total}\n" -o /dev/null http://localhost:8080/demo1/employee/hello

그러나 무한 루프를 실행하고 응답을 확인합니다.

답변1

아래 스크립트는 URL에 걸리는 시간을 확인하고 2분 이상이면 if 조건 내부로 들어갑니다. 거기에 다시 시작 명령을 입력해야 합니다. 각 반복마다 60초 동안 절전 모드로 유지됩니다.

#!/bin/bash

LOG_FILE=/tmp/log.txt

while true
do
    echo "$(date) - Checking the URL is up or not" >> ${LOG_FILE}
    TIME_TAKEN=$(curl -s -w "%{time_total}\n" -o /dev/null http://localhost:8080/demo1/employee/hello)
    if [ "${TIME_TAKEN}" -gt "120" ]
    then
        echo "$(date) - Restart required. Time taken is ${TIME_TAKEN}" >> ${LOG_FILE}
        # your restart command goes here
        echo "$(date) - Successfully restarted" >> ${LOG_FILE}
    fi
    echo "$(date) - Sleep for 60 seconds" >> ${LOG_FILE}
    sleep 60
done

관련 정보