Advertisement

Shell Script For Website Uptime Checker Script

Linux Shell Script For Website Uptime Checker Script

Website Uptime Checker

#!/bin/bash
website="https://example.com"
# Check if website is accessible
if curl --output /dev/null --silent --head --fail "$website"; then
echo "Website is up."
else
echo "Website is down."
fi

Shell script to check the uptime of a website involves sending HTTP requests to the site and analyzing the response.

#!/bin/bash

# Function to check website status
check_website() {
    SITE=$1
    STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" $SITE)

    if [ $STATUS_CODE -eq 200 ]; then
        echo "Website $SITE is UP (HTTP status $STATUS_CODE)"
    else
        echo "Website $SITE is DOWN (HTTP status $STATUS_CODE)"
    fi
}

# Define websites to check
WEBSITES=(
    "http://example.com"
    "https://google.com"
    "https://github.com"
)

# Loop through each website and check its status
for site in "${WEBSITES[@]}"; do
    echo "Checking $site ..."
    check_website "$site"
    echo
done

echo "Website uptime check complete."

Explanation:

  1. Shebang: #!/bin/bash specifies that this script should be interpreted using the Bash shell.

  2. check_website Function: Uses curl to send an HTTP request (-s for silent, -o /dev/null to suppress output, -w "%{http_code}" to print HTTP status code) to the specified website ($SITE). The HTTP status code ($STATUS_CODE) is then checked:

    • If STATUS_CODE is 200, the website is considered UP.
    • Otherwise, it's considered DOWN.
  3. WEBSITES Array: Contains a list of websites (http://example.com, https://google.com, https://github.com) that you want to monitor. Add or remove websites as needed.

  4. Loop: Iterates through each website in the WEBSITES array, calling check_website function for each.

Usage:

  • Save this script to a file, e.g., check_website_uptime.sh.
  • Make it executable with chmod +x check_website_uptime.sh.
  • Run it with ./check_website_uptime.sh.

Notes:

  • Customization: Modify the WEBSITES array to include the URLs of the websites you want to monitor.

  • Error Handling: This script assumes basic error handling. You may want to add more robust error checking and reporting based on your specific requirements.

  • Security: Be mindful of rate limiting and impact on the monitored websites. Use appropriate intervals between checks to avoid overwhelming the servers.

This script provides a basic framework for checking the uptime status of websites using HTTP status codes. Depending on your needs, you can enhance it with features like email alerts, logging, or integration with monitoring systems for more comprehensive website monitoring.





Post a Comment

0 Comments