schedule
schedule copied to clipboard
How would a schedule a job to be run every 5 minutes between two times, every day
Hey all, Apologies if this is stupid simple - I am new to the library. I am trying to schedule a job using this library which can execute a function:
- Every day,
- Between 7:00 am and 9:00 am
- Every 5 minutes In other words, I am trying to schedule those 24 instances when the job should be executed.
PS: trying to make a logger for daily commute using the google maps api in case folks are interested.
- Have the job start at 7:00 am every day.
- Have a second job that turns on inside the first job when the 7:00 am job runs and the second job runs every 5 minutes.
- Have the second job cancel itself when 2 hours have been reached. (i.e., inside the second job. Normally the job will reschedule itself for the next 5min unless it is canceled.
I had the exact same question and found this in the docs:
Run a job until a certain time
import schedule
from datetime import datetime, timedelta, time
def job():
print('Boo')
# run job until a 18:30 today
schedule.every(1).hours.until("18:30").do(job)
# run job until a 2030-01-01 18:33 today
schedule.every(1).hours.until("2030-01-01 18:33").do(job)
# Schedule a job to run for the next 8 hours
schedule.every(1).hours.until(timedelta(hours=8)).do(job)
# Run my_job until today 11:33:42
schedule.every(1).hours.until(time(11, 33, 42)).do(job)
# run job until a specific datetime
schedule.every(1).hours.until(datetime(2020, 5, 17, 11, 36, 20)).do(job)
I found this from an earlier question:
def job():
print("I'm working %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
def control_job():
#run between Mon. to Fri. and 09 ~ 18 hour/day
tnow = datetime.datetime.now()
hh = tnow.strftime('%H') #hour like '09'
wk = tnow.isoweekday() #Mon =1
if (('08'<hh<'18') and (1 <=wk <=5)):
job()
schedule.every().hour.do(control_job)
```
why author have not make it as a API, it is only a simple judge work.