[英]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.