Pavan Kulkarni
Pavan Kulkarni

Reputation: 476

Execute python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri

I need a way to execute my python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri.

I have explored APScheduler with cron syntax but can't figure out how to code the above condition. I tried below but doesn't work (execute is my function name)

sched.add_cron_job(execute, day_of_week='mon-fri', hour='9:30:42-15:00:42', minute='*/15')

Any pointer is deeply appreciated.

Upvotes: 1

Views: 3887

Answers (2)

Pavan Kulkarni
Pavan Kulkarni

Reputation: 476

I wrote custom code to solve my problem. Posting here in case it helps someone. Any optimisations suggestions are welcome.

The first infinite loop starts the job when the start time is hit. The 2nd infinite loop wakes up every x minutes to check if next run time has approached. If yes, it executes else goes back to sleep. If the end time for execution has reached, then it breaks out

def execute_schedule_custom():
start_time_of_day = datetime.combine(date.today(), time(9, 30, 42))
next_run_time = start_time_of_day
end_time_of_day = datetime.combine(date.today(), time(15, 0, 42))

interval = 15
sleep_secs = 60 * 5 #sleep for 5 mins

while True:
    if datetime.now() >= start_time_of_day:
        execute()
        next_run_time = start_time_of_day + timedelta(minutes=interval)
        break

while True:
    if datetime.now() >= end_time_of_day:
        break
    elif datetime.now() >= next_run_time:
        execute()
        next_run_time = next_run_time + timedelta(minutes=interval)
    t.sleep(sleep_secs)

Upvotes: 1

followingell
followingell

Reputation: 501

As far as I can tell you won't be able to do what you want with a single job.

This is the closest I could get with one:30-59/15 9-14 * * 1-5 which equates to Every 15 minutes, minutes 30 through 59 past the hour, between 09:00 AM and 02:59 PM, Monday through Friday.

Although it isn't exactly what you wanted I hope this helps as a base.

Upvotes: 1

Related Questions