Skip to content

Instantly share code, notes, and snippets.

@Inkimar
Last active June 8, 2023 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inkimar/c68f2e62fd20b288f473950573857fd4 to your computer and use it in GitHub Desktop.
Save Inkimar/c68f2e62fd20b288f473950573857fd4 to your computer and use it in GitHub Desktop.
bash script, HTTP-STATUS is 200 or NOT
a bash-script for checking if a website is up (HTTP STATUS == 200) , if not, then send an email.
- token is needed for gmail,
- - see here (1) https://support.google.com/mail/answer/185833?hl=en or (2) https://web.archive.org/web/20230521225549/https://support.google.com/mail/answer/185833?hl=en (that is internet-archive is (1) is not available)
1. a bash-script ends with 'sh' -> ```bash-script-name.sh```
2. you need to make the script executable by running `chmod +x bash-script-name.sh`
date = 2023-06-08
@Inkimar
Copy link
Author

Inkimar commented Jun 8, 2023

#!/bin/bash

# Email configuration
sender_email="<<replace>>"
receiver_email="<<replace>>"
subject="Website Down"
body="The website naturforskaren.se is down."

# Website configuration, here is the service you want to check
url="https://naturforskaren.se"

# Gmail SMTP configuration
smtp_server="smtp.gmail.com"
smtp_port="465"
smtp_username="<<replace>>"
smtp_password="<<replace>>"

while true; do
  response=$(curl -s -o /dev/null -w "%{http_code}" "$url")

  if [[ $response -eq 200 ]]; then
    echo "$(date): The website is up!"
  else
    echo "$(date): The website is down. Response code: $response"

    # Sending email using curl and Gmail SMTP
    curl --url "smtps://$smtp_server:$smtp_port" --ssl-reqd \
      --mail-from "$sender_email" --mail-rcpt "$receiver_email" \
      --upload-file <(cat <<EOF
From: $sender_email
To: $receiver_email
Subject: $subject

$body
EOF
) --user "$smtp_username:$smtp_password" --insecure
  fi

  sleep 60  # Wait for 60 seconds before the next check
done
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment