[英]How to run a python function every day at specific time
I am trying to run a specific function every day at 18:00, I wrote the folwing code to do that but it did nothing我试图在每天 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()
I do not want to use cron
, I want my script to be OS independent我不想使用
cron
,我希望我的脚本独立于操作系统
cron
and related applications are the basic way to do this. cron
和相关应用程序是执行此操作的基本方法。 With your approach, you have to keep the Python interpreter running 24/7, and if it crashes, all your times are off.使用您的方法,您必须保持 Python 解释器 24/7 全天候运行,如果它崩溃,您的所有时间都将关闭。
I found the answer here https://stackoverflow.com/a/16786600/9308420我在这里找到了答案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.