BASH

Monitor Website Availability with Bash

A Bash script to periodically check if a website is online and notify if it's down, useful for site reliability and basic health checks.

#!/bin/bash

URL="https://example.com"
HTTP_CODE=$(curl -s -o /dev/null -w "%{\http_code}" "$URL")

if [ "$HTTP_CODE" -eq 200 ]; then
  echo "$(date): $URL is UP (HTTP $HTTP_CODE)"
else
  echo "$(date): $URL is DOWN (HTTP $HTTP_CODE)"
  # Add notification logic here, e.g., send an email or Slack message
  # echo "Website down!" | mail -s "Website Alert" [email protected]
fi
How it works: This script uses `curl` to perform a silent HTTP GET request to a specified URL and captures the HTTP status code. It then checks if the status code is 200 (OK). Based on the result, it prints a message indicating whether the website is up or down, providing a basic health monitoring solution.

Need help integrating this into your project?

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

Hire DigitalCodeLabs