1

I want to check the web server health status. If did get http-response in 2 minutes then the server should restart. I am using the below command to check the status -

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

But executes the infinite loop and check for a response.

  • what is the output of the mentioned curl command ? it gives the output in seconds ? you want to run the curl command infinity times and sleep for every iteration ? – Kamaraj Jun 6 at 10:28
2

The below script will check the time taken by the url and if it's more than 2 minutes, then it will go inside the if condition. you have to put your restart command there. It sleeps for 60 seconds for each iteration

#!/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
  • Thanks for the response, kamalraj. How to test the above script. The API (demo1/employee/hello) sends the response after 10 minutes. I intently slept of 10 minutes to test my scenario. The if-condition will check after 10 minutes. How to test the above script. – user2286733 Jun 6 at 11:34
  • if i understand correctly, your curl command takes 10 minutes to execute the API. so TIME_TAKEN will have the value of 600+ . Then we are checking whether the time taken is greater than 120, then we have to restart the app server – Kamaraj Jun 7 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.