
Web サーバーの健全性ステータスを確認したいと思います。2 分以内に http-response を取得した場合、サーバーは再起動する必要があります。ステータスを確認するには、以下のコマンドを使用します。
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