[英]How to run a python function every day at specific time
我试图在每天 18:00 运行一个特定的函数,我写了以下代码来做到这一点,但它什么也没做
today = datetime.today()
nextDay = (today + timedelta(days=1)).replace(hour=18, minute=0, second=0)
delta_time = nextDay - today
secs = delta_time.seconds + 1
timer = Timer(secs, self.send_officer_report(".send officer report --ad " + today.strftime("%Y/%m/%d"), today.strftime("%Y/%m/%d")))
timer.start()
我不想使用cron
,我希望我的脚本独立于操作系统
cron
和相关应用程序是执行此操作的基本方法。 使用您的方法,您必须保持 Python 解释器 24/7 全天候运行,如果它崩溃,您的所有时间都将关闭。
我在这里找到了答案https://stackoverflow.com/a/16786600/9308420
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
while 1:
schedule.run_pending()
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.