
我想檢查網路伺服器的健康狀態。如果在 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