Shell : Shell script that checks the status of a specific web page

Shell Scripting @ Freshers.in

Here we will explain a shell script that checks the status of a specific web page. It could be used to monitor whether a web page is accessible and responding correctly.

Shell script:

#!/bin/bash

# Check if URL has been provided to check 
if [ $# -ne 1 ]
then
    echo "Usage: $0 <url>"
    exit 1
fi

URL="$1"

# Use curl to fetch the HTTP status code of web
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" $URL)

# Check the status code from web
if [ "$HTTP_STATUS" -eq "200" ]
then
    echo "Website $URL is up and running and looks Good !"
else
    echo "Website $URL is not responding correctly and is inaccessible . HTTP status: $HTTP_STATUS"
fi
Here’s what this script does, step by step:
  1. The script expects a URL to be passed to it as a command-line argument. If no argument is provided, it outputs a usage message and exits.
  2. It assigns the provided URL to the URL variable for later use.
  3. The script then uses the curl command to make a request to the provided URL. The -s option silences curl‘s progress output, the -o /dev/null option discards the content of the web page, and the -w “%{http_code}” option tells curl to output the HTTP status code of the response. This status code is assigned to the HTTP_STATUS variable.
  4. The script checks the value of the HTTP_STATUS variable. If it’s 200, which is the HTTP status code for a successful response, the script outputs a message saying that the website is up and running. If the status code is anything other than 200, it means there was some kind of error, and the script outputs a message saying that the website is not responding correctly, along with the actual HTTP status code.
  5. This script could be used to monitor the availability of a web page. You might run it manually when you need to check a page, or schedule it to run regularly using a tool like cron. If the script is run regularly, you might want to extend it to send an email or other kind of notification when the page is not responding correctly, rather than just outputting a message to the console.

This script could be used to monitor the availability of a web page. You need to schedule it to run regularly using a tool like cron.

Other urls to refer

Author: user

Leave a Reply