-2

I need to check availability of a specific website. I am trying to write a Python script that will ping a website every 15 minutes. If the website is down, I would like it to output "website is not available" and send an email notifying the user that the website is down.

I am pretty new to python and I do not know where to start.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options




chrome_options = Options()
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(options=chrome_options, executable_path="C:/Users/TX394UT/Desktop/Web_Bot_Project/chromedriver.exe")
driver.get("https://www.google.com/")
print(driver.title)
driver.close()
driver.quit()
print("Completed Sucessfully")

If This site can’t be reached, I would like for it to send me an email notification. I would like for the script to run every 15 minutes.

Stephen Tham
  • 81
  • 1
  • 1
  • 5
  • 2
    Why would you use selenium for this? – Daniel Roseman Jul 26 '19 at 20:52
  • 1
    @DanielRoseman I don't disagree with your sentiment... perhaps you can suggest a better alternative? – JeffC Jul 27 '19 at 01:06
  • Website Pulse or Pingdom Tools is probably better suited over Selenium. If you want to use Selenium, I would setup Jenkins and run a job every 15 minutes and send an email with results. I would just make sure when you hit google for example you assert that the logo is present.The problem with this solution is you need to open the email to see the result. You could setup a rule to ignore the email if it has not passed and delete. Your question is really more suited for Pingdom or Website Pulse though inho. – Dazed Jul 27 '19 at 15:42
  • Don't use selenium for this simple task – dnorhoj Dec 09 '19 at 10:41

2 Answers2

0

Schedule tasks to run in the background

On Linux

The simplest way to schedule a background script on Linux is by using cron and a crontab generator such as crontab-generator.org

For example to run a python script every 15 minutes it would look like this:

15 * * * * python /home/user/scripts/examplescript.py >/dev/null 2>&1

On Windows

On windows, you can use the built-in "Windows Task Scheduler" which is explained in another stackoverflow answer.


Also, it's not very smart to use selenium to check if a website is functional, try checking out python requests which is a very nice library to make HTTP requests.

dnorhoj
  • 128
  • 1
  • 10
0

I like to use the Requests library to detect if a website is down. Use a while loop and the time.sleep function to run the code every 15 minutes.

import requests
import time

while True:
    try:
        requests.get('https://www.google.com/')
        # insert code that runs when site is up here
    except:
        # insert code that runs when site is down here

    time.sleep(900)
Ham Sandwich
  • 11
  • 1
  • 1