BASH
Basic Web Service Availability Check
Implement a simple Bash script to check the availability of a web service by making a GET request and verifying the HTTP status code (e.g., 200 OK).
#!/bin/bash
# --- Configuration ---
TARGET_URL="http://your-web-service.com/health"
EXPECTED_STATUS="200"
# Perform a GET request and capture HTTP status code
HTTP_STATUS=$(curl -s -o /dev/null -w "%{\http_code}" "${TARGET_URL}")
# Check if the status matches the expected one
if [ "${HTTP_STATUS}" -eq "${EXPECTED_STATUS}" ]; then
echo "Service ${TARGET_URL} is UP. Status: ${HTTP_STATUS}"
exit 0
else
echo "Service ${TARGET_URL} is DOWN or returned unexpected status. Status: ${HTTP_STATUS}" >&2
exit 1
fi
How it works: This script performs a basic health check on a web service. It uses `curl` to send a GET request to a specified `TARGET_URL`. The `-s` flag makes `curl` silent, `-o /dev/null` discards the output body, and `-w "%{\http_code}"` tells `curl` to print only the HTTP status code. The script then compares this status code to an `EXPECTED_STATUS` (e.g., 200 for success) and reports whether the service is considered "UP" or "DOWN".