BASH

Monitor Website Availability and Response Time

Use `curl` to perform essential health checks on a website. Verify its HTTP status code and measure connection and total response times for critical performance monitoring.

#!/bin/bash

URL="https://example.com/"

echo "Checking URL: $URL"

CURL_OUTPUT=$(curl -s -o /dev/null -w "%{\http_code}\
%{\time_total}\
%{\time_connect}" "$URL")

HTTP_STATUS=$(echo "$CURL_OUTPUT" | sed -n '1p')
TOTAL_TIME=$(echo "$CURL_OUTPUT" | sed -n '2p')
CONNECT_TIME=$(echo "$CURL_OUTPUT" | sed -n '3p')

if [ "$HTTP_STATUS" -eq 200 ]; then
  echo "Website is UP (HTTP Status: $HTTP_STATUS)"
  echo "Total Response Time: ${TOTAL_TIME}s"
  echo "Connection Time: ${CONNECT_TIME}s"
else
  echo "Website is DOWN or returned error (HTTP Status: $HTTP_STATUS)"
  echo "Total Response Time: ${TOTAL_TIME}s"
fi
How it works: This script uses `curl` to check the availability and performance of a given URL. It retrieves the HTTP status code, total response time, and connection time without displaying the page content. It then parses these values and reports whether the website is up or down, along with key performance metrics.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs