【问题标题】:Cronjob doesn't execute python scriptCronjob 不执行 python 脚本
【发布时间】:2021-01-22 12:20:39
【问题描述】:

我想在一天中的每个小时都使用 Cron 来执行我的 python 脚本。因此我创建了一个看起来像这样的 cronjob:@hourly /home/pi/Desktop/repository/auslastung_download/auslastung.py

cronjob 应该执行以下脚本:

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())

当通过python3 auslastung.pyeverything 执行时,一切正常,脚本写入 data.csv 文件。

也许有人可以帮助我:)

【问题讨论】:

  • 脚本顶部没有#!/usr/bin/python,因此您需要在指向脚本路径之前调用python3
  • 你错过了脚本第一行的 shebang ...类似于#!/usr/bin/env python
  • 我添加了 #!/usr/bin/python bur,它仍然无法正常工作。我也对文件做了 chmod +x
  • 尝试查找/usr/bin/python 是否是您的python 解释器。使用which python3 找到它。
  • usr/bin/python是根据which python的解释器

标签: python linux cron raspberry-pi


【解决方案1】:

首先你必须确保你的脚本运行。

如果您以交互方式运行 python3 auslastung.py,为什么您在 cron 上以不同方式调用您的 python 脚本。

您是否尝试过仅以交互方式运行 /home/pi/Desktop/repository/auslastung_download/auslastung.py?没有初始python3,它会运行吗?

如果您的脚本在您的 crontab 上使用 python3 auslastung.py 运行,您应该包含解释器和脚本的完整路径:

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

如果您让脚本直接运行而无需指明解释器,只需 /full/path/to/script.py 那么在您的 crontab 中您应该包含脚本的完整路径:

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

您可以包含一个 shebang:脚本的第一行表明使用哪个解释器来执行它。所以你的第一行应该是#!/path/to/your/interpreter

您必须确保该脚本具有chmod +x auslastung.py 的执行权限。

【讨论】:

  • 是的,它在没有初始 python3 命令的情况下运行。所以它应该是一个可执行文件,因为我已经做了chmod +x。我的 crontab 条目看起来与您描述的完全一样。我首先输入了crontab -e,然后输入了@hourly /path/to/script.py。当使用crontab -l列出工作时,我也看到了我的条目,但它不会每小时运行一次脚本
猜你喜欢
  • 2016-08-21
  • 2011-03-29
  • 1970-01-01
  • 2015-11-12
  • 2017-12-01
  • 2015-04-17
  • 2019-02-25
相关资源
最近更新 更多