← Back to all snippets
BASH

Check Website Availability and HTTP Status

Monitor your web services by checking website availability and HTTP status codes using a simple Bash script with `curl` to ensure uptime and health.

#!/bin/bash

URL="https://www.google.com"
EXPECTED_STATUS=200

echo "Checking URL: $URL"

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

if [ "$HTTP_STATUS" -eq "$EXPECTED_STATUS" ]; then
  echo "Success: Website is UP and returned HTTP $HTTP_STATUS"
  exit 0
else
  echo "Error: Website is DOWN or returned HTTP $HTTP_STATUS (Expected: $EXPECTED_STATUS)"
  exit 1
fi
How it works: This script uses `curl` to check the availability and HTTP status code of a specified URL. The `-s` flag silences progress output, `-o /dev/null` discards the page content, and `-w "%{\http_code}"` tells `curl` to output only the HTTP status code. The script then compares this status with an `EXPECTED_STATUS` to determine if the website is healthy.

Need help integrating this into your project?

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

Hire DigitalCodeLabs