Python 脚本计划在每个月的第 1 天 2 :00 am 运行

标签 python python-2.7 scheduling

我有一个每 30 分钟运行一次的脚本,但有一个部分我只想在每月的第一天凌晨 2:00 运行。我在 Python 中使用 schedule,但我不知道如何将它设置为每月的第 1 天。

month 似乎不在 schedule 的定义参数中以执行类似 schedule.every().month.at("02: 00").do(job2)

有什么建议吗?我正在使用 python 2.7

简化代码:

from safe_schedule import SafeScheduler
import time

def job():
    print "I'm working...",
    return

def scheduler():
    # Schedule every30min routines
    print 'Starting Scheduler'
    scheduler = SafeScheduler()
    scheduler.every(30).minutes.do(job)
    #scheduler.every().month.at("02:00").do(job2)

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


if __name__ == '__main__':
     scheduler()

最佳答案

库的主要贡献者不鼓励这种事情,参见 https://github.com/dbader/schedule/issues/73#issuecomment-167758653 .

然而,如果有人坚持,可以安排每日作业,但仅在每月 1 日运行。

from datetime import date

from safe_schedule import SafeScheduler


def job2():
    if date.today().day != 1:
        return

    # actual job body


scheduler = SafeScheduler()
scheduler.every().day.at("02:00").do(job2)

问题评论之一描述了另一种选择 https://github.com/dbader/schedule/issues/73#issuecomment-356769023 .

关于Python 脚本计划在每个月的第 1 天 2 :00 am 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221153/

相关文章:

python - 打开 IPython 笔记本时出错

python - 如何使用另一个 numpy 数组修改 pandas 数据框中的所有值

python 2.7 : Listen to requested connections + listen to already established connections at the same time

algorithm - 协调排类与可用性

c - 使用SJF(SPN)算法推送队列

python - 为什么 os.system 不忽略 SIGINT?

python - wxPython 对话框 : "Enter" keyboard button would not "ok" the dialog

python-2.7 - 如何解决 PendingDeprecationWarning : the matrix subclass is not the recommended way to represent matrices 的问题

python - 使用 numpy.loadtxt 加载包含 float 和字符串的文本文件

java - 两个 Quartz-Worker 执行相同的作业两次