Search code examples
pythondatetimepython-3.6apscheduler

How to use day of the week (Tuesday) instead of using date (5-12-2000), to execute a program at particular time?


from datetime import date
from apscheduler.scheduler import Scheduler

 # Start the scheduler
 sched = Scheduler()
 sched.start()

 # Define the function that is to be executed
 def my_job(text):
     print text

 # The job will be executed on November 6th, 2009
 exec_date = date(2009, 11, 6)

 # Store the job in a variable in case we want to cancel it
 job = sched.add_date_job(my_job, exec_date, ['text'])

Solution

  • You can use cron style scheduling to add job on a specific day(s) of the week.

    Your add_date_job can be written as :

    sched.add_cron_job(my_job, args = ['text'], day_of_week='tue')
    

    Just a heads up, make sure to add specific time to your job as well, otherwise the scheduler would take the default hour,minute,and second as * and would trigger every second.

    Reference for apscheduler : https://apscheduler.readthedocs.io/en/stable/index.html