3
schedule.wednesday.at("13:15").do(job)

With this code I can run fuction on wednesday 13:15. But I want to make action like, "run function next day morning '10:00' " How can do this?

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Does this answer your question? [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) – thethiny Mar 25 '21 at 10:35
  • change your `wednesday` to `every().day` – thethiny Mar 25 '21 at 10:36
  • hmm,, not every day. Only the next day 10:00 I run the program –  Mar 25 '21 at 10:36
  • Get your current date, then offset to 10am then next day. – Amiga500 Mar 25 '21 at 10:37
  • I see what you mean now. Well you have to find out what day it is right now using `datetime` and call the function that is next day. Or use `timedelta` to add 1 day to today and replace the hour with `10:00`. – thethiny Mar 25 '21 at 10:38
  • Then the time type is going to be string? or datetime type? Can I see some example.? –  Mar 25 '21 at 10:40

3 Answers3

3

Wrap job to return schedule.CancelJob such that it runs only once. Then schedule it to run every day at 10:00.

def job_once():
    job()
    return schedule.CancelJob

schedule.every().day.at('10:00').do(job_once)

Caveat: if the current time is before 10:00, then the job will run this day at 10:00. There's no way to define an initial delay in the schedule library - but we could get the name of the next day and schedule on this day.

import datetime
tomorrow = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%A').lower()
getattr(schedule.every(), tomorrow).at('10:00').do(job_once)
timgeb
  • 76,762
  • 20
  • 123
  • 145
1

Python contains a simple library called sched:

import sched, time

def functionYouWantToRun():
    ...

s = sched.scheduler(time.localtime, time.sleep)
s.enterabs(time.strptime('[three character day] [three character month] [day of month] [time (hh:mm:ss)] [year]')
s.run()
0
def job_that_executes_once():
    # Do some work that only needs to happen once...
    return schedule.CancelJob


if __name__ == "__main__":
    from datetime import datetime, timedelta
    import schedule
   
    schedule.every().day.at("10:00").do(job_that_executes_once)

I was going to go with time offset, but then realised the above is easier. That will run it the next day.

If you need to run several days in the future, then you are back to datetime offsets:

    x=datetime.today()
    y = x.replace(day=x.day, hour=10, minute=0, second=0, microsecond=0) + 
    timedelta(days=1)
    yDay = y.strftime('%A')
Amiga500
  • 1,258
  • 1
  • 6
  • 11