• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Downtime Cron that sends an email?

wyclef

New Coder
Hey, wondering if anyone has any ideas that might be better than this one I yanked off Github. Looking for a bash script I can run a cron on several times a day that auto checks a list of domains to see if any are down.

Bash:
#!/bin/bash

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

for url in "${urls[@]}"
do
  /usr/bin/wget --server-response -O /dev/null $url > /dev/null 2> /dev/null
  if [ $? -ne 0 ]
  then
    # Full URLs can disappear in text messages. Extract the domain and use that
    # for all error reporting instead.
    domain=`echo $url | awk -F/ '{print $3} down'`

    # If the website appears down, check again in five minutes to eliminate
    # false positives.
    echo "$domain down. Waiting 5 minutes."
    sleep 300

    /usr/bin/wget --server-response -O /dev/null $url > /dev/null 2> /dev/null
    if [ $? -ne 0 ]
    then
      echo "$domain still down. Sending email and/or text messages."
      echo "$domain down" | /usr/sbin/sendmail [email protected]
    fi
  fi
done
 

Antero360

Active Coder
Hey, wondering if anyone has any ideas that might be better than this one I yanked off Github. Looking for a bash script I can run a cron on several times a day that auto checks a list of domains to see if any are down.

Bash:
#!/bin/bash

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

for url in "${urls[@]}"
do
  /usr/bin/wget --server-response -O /dev/null $url > /dev/null 2> /dev/null
  if [ $? -ne 0 ]
  then
    # Full URLs can disappear in text messages. Extract the domain and use that
    # for all error reporting instead.
    domain=`echo $url | awk -F/ '{print $3} down'`

    # If the website appears down, check again in five minutes to eliminate
    # false positives.
    echo "$domain down. Waiting 5 minutes."
    sleep 300

    /usr/bin/wget --server-response -O /dev/null $url > /dev/null 2> /dev/null
    if [ $? -ne 0 ]
    then
      echo "$domain still down. Sending email and/or text messages."
      echo "$domain down" | /usr/sbin/sendmail [email protected]
    fi
  fi
done
why not use ping?
 
why not use ping?
Ping is simply an indication that ICMP (Internet Control Message Protocol) is functioning and it does not provide any clue as to whether or not the webserver is responding to requests or that a particular page is loading correctly. This tool is actually checking the webserver itself.


The code isn't that bad, really. It could be cleaned up, though. However, my #1 choice would be to pay for statuspage (the service, not the typo).
 

Top