Title: Building a Website Status Monitoring Shell Script with Email Notifications

In today's digitally connected world, the availability of websites and online services is paramount. For both business owners and users, knowing when a website is down is crucial. One way to achieve this is by creating a shell script that monitors the availability of a list of websites and sends email notifications when any of them become unresponsive. In this blog post, we'll guide you through creating a simple yet effective website monitoring shell script.

Step 1: Set Up Your Environment

Before diving into the code, let's prepare our environment:

  1. Linux Environment: Make sure you are working in a Linux environment, as we'll be using shell scripting.

  2. Install Required Tools: Ensure you have the necessary tools installed, including curl for sending HTTP requests and a way to send emails from your Linux system.

Step 2: Create the Script

Now, let's create the shell script to monitor websites. We'll name the script website_monitor.sh. You can use your favorite text editor to create it.

#!/bin/bash

# Define a function to check the website status
check_website() {
    local url="$1"
    local response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")

    if [ "$response_code" != "200" ]; then
        echo "Website $url is down (HTTP $response_code)!"
        # Add code here to send an email notification
    fi
}

# List of websites to monitor
websites=(
    "https://example.com"
    "https://google.com"
    # Add more websites to the list
)

# Loop through the list and check each website
for website in "${websites[@]}"; do
    check_website "$website"
done

In the script, we define a function check_website that takes a URL as an argument. It uses curl to make an HTTP request to the URL and checks the HTTP response code. If the response code is not 200, it means the website is down, and the script echoes a message.

Step 3: Specify Websites to Monitor

Next, specify the websites you want to monitor by adding their URLs to the websites array. You can add as many websites as you want to this list.

Step 4: Add Email Notifications

In the script, there's a placeholder for sending email notifications. This part is essential for getting notified when a website is down. You can use a command-line tool like mail or sendmail to send emails. Be sure to configure your system's email settings for this step.

Step 5: Schedule the Script

To run the script at regular intervals, you can set up a cron job. For example, to check websites every 10 minutes, add the following line to your crontab:

*/10 * * * * /path/to/website_monitor.sh

This line schedules the script to run every 10 minutes.

Conclusion

With this simple shell script, you can monitor the availability of your favorite websites or critical online services. Whenever a website goes down, you'll receive an email notification. This basic script can be extended to meet your specific needs, such as more advanced alerting, logging, and reporting. It's a valuable tool for ensuring the reliability of your online resources. Happy scripting!