简体   繁体   English

如何每天在特定时间运行python函数

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

相关问题 如何在python每天特定时间运行一个function? - How to run a function every day at a specific time in python? Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何使用 python 让 Discord 机器人每天在特定时间运行 function? - How to make Discord bot run a function every day at a specific time using python? 在 Heroku 上部署时,每天在特定时间运行函数的 Python 脚本不起作用 - Python script to run a function at a specific time every day does not work when deployed on Heroku Discord.py Bot 每天特定时间运行功能 - Discord.py Bot run function at specific time every day 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何安排代码在每天的特定时间运行? - How to schedule code to run at a specific time every day? 如何在每天的特定时间持续运行Google合作实验室? - How to constantly run Google Colaboratory at a specific time every day? 如何安排生产 python 脚本以触发 function 在每天的指定时间运行 - How to schedule a production python script to trigger a function to run at a specified time every Day python-如何在每个月的最后一天运行功能? - python - how to run a function on last day of every month?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM