{"id":17058,"date":"2024-05-23T11:08:38","date_gmt":"2024-05-23T11:08:38","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=17058"},"modified":"2024-05-24T07:43:42","modified_gmt":"2024-05-24T07:43:42","slug":"shell-script-to-send-email-if-website-down-gcp","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/23\/shell-script-to-send-email-if-website-down-gcp\/","title":{"rendered":"Shell Script to send email if multiple websites are down (gmail)."},"content":{"rendered":"\n<p>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\u2019s essential to be promptly notified so that necessary actions can be taken to restore its functionality.<\/p>\n\n\n\n<p>One effective way to achieve this is by automating email notifications to alert system administrators or stakeholders about the status of URLs. We\u2019ll utilize shell scripting to create a simple yet effective solution for monitoring website health and ensuring timely notifications in case of any disruptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AWS Account with Ubuntu 24.04 LTS EC2 Instance.<\/li>\n\n\n\n<li>Basic knowledge of Shell scripting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #1:Generate the App Password<\/strong><\/h3>\n\n\n\n<p>First go to your account.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"489\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" class=\"wp-image-17059\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-300x143.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-768x367.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Search for\u00a0<strong>App Password<\/strong>\u00a0in the search bar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"317\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-1.png\" alt=\"\" class=\"wp-image-17060\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-1.png 905w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-1-300x105.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-1-768x269.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Enter your google account password to verify it\u2019s you.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"475\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-2.png\" alt=\"\" class=\"wp-image-17061\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-2.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-2-300x139.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-2-768x356.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Now Enter the app name for which you wanted to get the app password like&nbsp;<strong>URL Notification.<\/strong><\/p>\n\n\n\n<p>And click on\u00a0<strong>Create\u00a0<\/strong>to create it<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"487\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-3.png\" alt=\"\" class=\"wp-image-17062\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-3.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-3-300x143.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-3-768x365.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>the password will be generated. Note it down cause will be using it<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"560\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-4.png\" alt=\"\" class=\"wp-image-17063\" style=\"width:674px;height:auto\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-4.png 675w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/image-4-300x249.png 300w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #2:Create a file in Ubuntu<\/strong><\/h3>\n\n\n\n<p>Open the terminal and use the&nbsp;<strong>nano&nbsp;<\/strong>command to create a new file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>nano url_notify.sh<\/strong>\n----------------------------------------------------\n<strong>Step #3:Write a Script to Send Email Notification<\/strong>\n\nBelow is a basic script to send an email notification if any of the url\u2019s are down:\n----------------------------------------------------\n<strong>#!\/bin\/bash\n\n# Prompt the user for input\nread -p \"Enter your email: \" sender\nread -p \"Enter recipient email: \" receiver\nread -s -p \"Enter your Google App password: \" gapp\necho\n\n# List of URLs to check\nurls=(\"https:\/\/www.example.com\" \"https:\/\/www.google.com\" \"https:\/\/www.openai.com\")\n\n# Function to check the status of URLs and send email notification if any are down\ncheck_urls_and_send_email() {\n    local down_urls=\"\"\n    local subject=\"Website Down\"\n    for url in \"${urls&#91;@]}\"; do\n        response=$(curl -Is \"$url\" | head -n 1)\n        if &#91;&#91; ! $response =~ \"200\" ]]; then\n            down_urls+=\"$url\\n\"\n        fi\n    done\n    if &#91;&#91; -n $down_urls ]]; then\n        body=\"The following websites are down:\\n\\n$down_urls\"\n        email_content=\"From: $sender\\nTo: $receiver\\nSubject: $subject\\n\\n$body\"\n        response=$(curl -s --url 'smtps:\/\/smtp.gmail.com:465' --ssl-reqd \\\n            --mail-from \"$sender\" \\\n            --mail-rcpt \"$receiver\" \\\n            --user \"$sender:$gapp\" \\\n            -T &lt;(echo -e \"$email_content\"))\n        if &#91; $? -eq 0 ]; then\n            echo \"Email sent successfully.\"\n        else\n            echo \"Failed to send email.\"\n            echo \"Response: $response\"\n        fi\n    else\n        echo \"All websites are up.\"\n    fi\n}\n\n# Call the function to check URLs and send email\ncheck_urls_and_send_email<\/strong>\n-------------------------------------------------<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"632\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-6.png\" alt=\"\" class=\"wp-image-17064\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-6.png 662w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-6-300x286.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/figure>\n\n\n\n<p>Save the file and exit the editor.<\/p>\n\n\n\n<p><strong>Explanation of the Script:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>User Input Prompt:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The script prompts the user to enter their email address (<strong>sender<\/strong>), recipient\u2019s email address (<strong>receiver<\/strong>), and Google App password (<strong>gapp<\/strong>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>List of URLs:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The script defines an array\u00a0<strong>urls\u00a0<\/strong>containing the URLs to be checked for their status. These URLs represent the websites whose availability we want to monitor.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function to Check URLs and Send Email:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The\u00a0<strong>check_urls_and_send_email()<\/strong>\u00a0function iterates through each URL, using\u00a0<strong>curl<\/strong>\u00a0to 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.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Call:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The script calls the\u00a0<strong>check_urls_and_send_email<\/strong>\u00a0function to initiate the process of checking URL statuses and sending email notifications.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #4:Make file executable<\/strong><\/h3>\n\n\n\n<p>Change the file permissions to make it executable using the&nbsp;<strong>chmod&nbsp;<\/strong>command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>chmod +x url_notify.sh<\/strong>\n\n<strong>Step #5:Run the script<\/strong>\nRun the script by executing the following command.\n\n<strong>.\/url_notify.sh<\/strong>\n\nYou will be prompted to enter the sender\u2019s email, recipient\u2019s email and Google App password:\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"62\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-4.png\" alt=\"\" class=\"wp-image-17065\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-4.png 455w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-4-300x41.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/figure>\n\n\n\n<p>You will receive the message&nbsp;<strong>Email sent successfully.<\/strong><\/p>\n\n\n\n<p>Now check the mail box to verify if you received the mail or not.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"906\" height=\"537\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/email-notification.png\" alt=\"\" class=\"wp-image-17066\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/email-notification.png 906w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/email-notification-300x178.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/email-notification-768x455.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.fosstechnix.com\/shell-script-to-send-email-if-website-down\/\" data-type=\"link\" data-id=\"https:\/\/www.fosstechnix.com\/shell-script-to-send-email-if-website-down\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s essential to be promptly notified so that necessary actions can be &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/23\/shell-script-to-send-email-if-website-down-gcp\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Shell Script to send email if multiple websites are down (gmail).&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17058","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=17058"}],"version-history":[{"count":3,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17058\/revisions"}],"predecessor-version":[{"id":17083,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17058\/revisions\/17083"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=17058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=17058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=17058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}