HTTP Status Code Checker: A Simple Shell Script Solution 2024

HTTP Status Code Checker: A Simple Shell Script Solution

As administrators and developers, HTTP Status Code Checker if your website or service goes down, it can lead to lost traffic, reduced user satisfaction, or even business impact. we’ll show you how to create a simple shell script to verify whether a URL works by checking its HTTP status code.

HTTP Status Code Checker Objectives :

  • To automate the process of checking if a URL is accessible.
  • To create a shell script that verifies the HTTP status code of a URL.
  • To understand basic debugging with set-x in-shell scripts.

Prerequisites :

  • Basic understanding of shell scripting.
  • A Unix-based system (Linux or macOS).
  • curl installed on your system.
ALSO READ : 

Notes :

  • URL Customization: Replace https://techbasehub.com/ with the URL you want to monitor.
  • Comments: The comments in the script are updated to be more descriptive and aligned with the script’s purpose.
  • Output Messages: Adjusted the output messages to be clear and informative about the URL’s status.

Feel free to modify the script further to suit your needs!

Example Output :

Enable debugging mode to trace script execution

out put

Script:

#!/bin/bash

# Enable debugging mode to trace script execution
set -x

# Define the URL to check
url="https://techbasehub.com/"
# Extract the hostname from the URL
hostname=$(echo $url | awk -F / '{print $3}')

# Function to check the HTTP status of the URL
check_url () {
# Fetch the HTTP status code using curl
local url_status_code=$(curl -I $url | awk 'NR==1 {print $2}')
# Check if the status code is 200 (OK)
if [[ $url_status_code -eq 200 ]]; then
echo -e "The URL $url is accessible.\nThe hostname is $hostname.\nStatus code: $url_status_code"
else
echo "The URL $url is not accessible. Status code: $url_status_code"
fi
}

# Call the function to perform the URL check
check_url

 

Make the Script Executable:

chmod +x url_check.sh

Run the Script:

./url_check.sh

Now you can HTTP Status Code Checker successfully check if your URL is accessible!

 

 

Leave a Comment