简体   繁体   中英

Cronjob doesn't execute python script

I want to use Cron to execute my python script every hour of the day. Therefore I created a cronjob that looks like: @hourly /home/pi/Desktop/repository/auslastung_download/auslastung.py

The cronjob should execute the following script:

from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver
from datetime import datetime, date


def get_auslastung_lichtenberg():
    try:
        url = "https://www.mcfit.com/de/fitnessstudios/studiosuche/studiodetails/studio/berlin-lichtenberg/"
        options = FirefoxOptions()
        options.add_argument("--headless")
        driver = webdriver.Firefox(options=options)
        driver.get(url)

        html_content = driver.page_source
        soup = BeautifulSoup(html_content, 'html.parser')

        elems = soup.find_all('div', {'class': 'sc-iJCRLp eDJvQP'})
        #print(elems)
        auslastung = str(elems).split("<span>")[1]
        #print(auslastung)
        auslastung = auslastung[:auslastung.rfind('</span>')]
        #print(auslastung)
        auslastung = str(auslastung).split("Auslastung ")[1]
        #print(auslastung)
        auslastung = auslastung[:auslastung.rfind('%')]
        print(auslastung)

        now = datetime.now()

        current_time = now.strftime("%H:%M:%S")
        #print("Current Time =", current_time)
        today = date.today()
        print(today)

        ergebnis = {'date': today, 'time':  current_time,'studio': "Berlin Lichtenberg", 'auslastung': auslastung}

        return ergebnis

    finally:
        try:
            driver.close()
        except:
            pass

"""
import json

with open('database.json', 'w') as f:
    json.dump(get_auslastung_lichtenberg(), f)
    """

import csv

with open('/home/pi/Desktop/repository/auslastung_download/data.csv', mode='a') as file:
    fieldnames = ['date', 'time', 'studio', 'auslastung']
    writer = csv.DictWriter(file, fieldnames=fieldnames)

    writer.writerow(get_auslastung_lichtenberg())

When executed via python3 auslastung.py everything works fine and the script writes into the data.csv file.

Maybe someone can help me :)

First of all you must ensure that your script runs.

If interactively you run python3 auslastung.py why do you invoke your python script differently on your cron.

have you tried to run just /home/pi/Desktop/repository/auslastung_download/auslastung.py interactively? without initial python3 , does it run?

If your script runs with python3 auslastung.py on your crontab you should include full path to both interpreter and script:

@hourly /paht/to/python3 /full/path/to/script.py

If you made your script to run directly without need to indicate interpreter, just /full/path/to/script.py then on your crontab you should include full path to script:

@hourly /full/path/to/script.py

You may include a shebang: a very first line of your script indicate which interpreter is used to execute it. So your first line should be #!/path/to/your/interpreter

An you have to ensure then that script has execute permision with chmod +x auslastung.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM