Shell Script to send email if website down

In this article, we will learn Shell Script to send email if website down, how to automate the process of checking the status of URLs and sending email notifications if any of them are down. When a website goes down or experiences issues, it’s essential to be promptly notified so that necessary actions can be taken to restore its functionality.

One effective way to achieve this is by automating email notifications to alert system administrators or stakeholders about the status of URLs. We’ll utilize shell scripting to create a simple yet effective solution for monitoring website health and ensuring timely notifications in case of any disruptions.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Basic knowledge of Shell scripting.

Step #1:Generate the App Password

First go to your account.

How to Send Email using Shell Script 1

Search for App Password in the search bar.

How to Send Email using Shell Script 2

Enter your google account password to verify it’s you.

How to Send Email using Shell Script 3

Now Enter the app name for which you wanted to get the app password like URL Notification.

And click on Create to create it

Shell Script to send email if website down 1

the password will be generated. Note it down cause will be using it.

How to Send Email using Shell Script 5

Step #2:Create a file in Ubuntu

Open the terminal and use the nano command to create a new file.

nano url_notify.sh
Shell Script to send email if website down 2

Shell Script to send email if website down

Step #3:Write a Script to Send Email Notification

Below is a basic script to send an email notification if any of the url’s are down.

#!/bin/bash

# Prompt the user for input
read -p "Enter your email: " sender
read -p "Enter recipient email: " receiver
read -s -p "Enter your Google App password: " gapp
echo

# List of URLs to check
urls=("https://www.example.com" "https://www.google.com" "https://www.openai.com")

# Function to check the status of URLs and send email notification if any are down
check_urls_and_send_email() {
    local down_urls=""
    local subject="Website Down"
    for url in "${urls[@]}"; do
        response=$(curl -Is "$url" | head -n 1)
        if [[ ! $response =~ "200" ]]; then
            down_urls+="$url\n"
        fi
    done
    if [[ -n $down_urls ]]; then
        body="The following websites are down:\n\n$down_urls"
        email_content="From: $sender\nTo: $receiver\nSubject: $subject\n\n$body"
        response=$(curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
            --mail-from "$sender" \
            --mail-rcpt "$receiver" \
            --user "$sender:$gapp" \
            -T <(echo -e "$email_content"))
        if [ $? -eq 0 ]; then
            echo "Email sent successfully."
        else
            echo "Failed to send email."
            echo "Response: $response"
        fi
    else
        echo "All websites are up."
    fi
}

# Call the function to check URLs and send email
check_urls_and_send_email
Shell Script to send email if website down 3

Save the file and exit the editor.

Explanation of the Script:

  1. User Input Prompt:
    • The script prompts the user to enter their email address (sender), recipient’s email address (receiver), and Google App password (gapp).
  2. List of URLs:
    • The script defines an array urls containing the URLs to be checked for their status. These URLs represent the websites whose availability we want to monitor.
  3. Function to Check URLs and Send Email:
    • The check_urls_and_send_email() function iterates through each URL, using curl to check their status. If any URLs are found to be down, it constructs and sends an email notification containing details of the down URLs to the specified recipient. The email is sent using SMTPS protocol with SSL, and the script handles the response to determine the success of the email sending operation.
  4. Function Call:
    • The script calls the check_urls_and_send_email function to initiate the process of checking URL statuses and sending email notifications.

Step #4:Make file executable

Change the file permissions to make it executable using the chmod command.

chmod +x url_notify.sh
Shell Script to send email if website down 4

Step #5:Run the script

Run the script by executing the following command.

./url_notify.sh
Shell Script to send email if website down 5

You will be prompted to enter the sender’s email, recipient’s email and Google App password.

Shell Script to send email if website down 6

Output:

Shell Script to send email if website down 7

You will receive the message Email sent successfully.

Now check the mail box to verify if you received the mail or not.

Shell Script to send email if website down 8

You can see the website down email like above.

Conclusion:

In conclusion, automating email notifications for URL status monitoring using shell scripting is a proactive approach to ensuring the continuous availability and performance of critical web services and applications. By implementing a simple shell script, organizations can establish an efficient mechanism for monitoring URL health and receiving timely alerts in case of any disruptions. By following the steps outlined in this article, organizations can enhance their website monitoring capabilities and minimize downtime, ultimately improving the overall reliability and user experience of their online services.

Related Articles:

How to Send Email using Shell Script

Reference:

Bash reference manual

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap