This blog will tell you how to write a script to check whether a given web service or URL is up or not by accessing a given URL.
Occasinally, your Apache (or other) web server might become unresponsive. So here we will get an idea how to write a script to detect this issue.
You can use “curl” or “wget” to access the given URL with a time out of 10 seconds. The output can be moved to /dev/null – just capture the HTTP return code. If the return code is not 200, declare the site is down.
retcode=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' $url -m 10)
down="false"
if [ "$retcode" != "200" ];then
down="true"
Next, you can have a cronjob that checks the health of the service or URL, let’s say, every minute.
I will give you an idea how to write the script:
Let’s say you have written your above ode snippets into “health_check.sh”.
Next, you have to invoke the script as part of the cronjob that runs every minute, i.e., it would be * * * * * /bin/health_check.sh
Thanks for reading.