安排作业仅在特定的一天在python中每5分钟运行一次

问题描述 投票:0回答:2

我只想在一周中的某一天每5分钟执行一次Cron作业。我正在使用python Schedule Library,可以单独完成这些工作。但是,我该如何俱乐部呢?

代码

import schedule

def check():
    print('Checking')

schedule.every(10).monday.do(check)
while True: 
    # Checks whether a scheduled task is pending to run or not 
    schedule.run_pending() 

[星期一尝试时出现错误IntervalError: Use mondays instead of monday,但出现错误AttributeError: 'Job' object has no attribute 'mondays'有人可以帮助我。

python scheduler schedule
2个回答
1
投票

我不认为schedule程序包支持您想要的不同时间单位的组合。您可以执行以下操作来实现所需的目标:

import schedule
import time
import datetime

def job():
    if datetime.datetime.today().weekday() == 0:
        print("I'm working...")

schedule.every(5).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

不过,也许不是最优雅的解决方案。


-1
投票

您尝试使用“时间”吗?

import time
...

schedule.every().monday.do(job)

while True:
    schedule.run_pending()
    time.sleep(300)
© www.soinside.com 2019 - 2024. All rights reserved.