Search code examples
python-3.xcron-taskapscheduler

Scheduling a cron job in python to run a python script every day at 10 am through APSCHEDULER


I want to schedule a cron job in python that runs a python script every day at 10 am. I am using apscheduler to achieve this functionality.

I am trying to use apscheduler functionality to schedule a cron job that runs every day at 10 am and executes a python script. But the job is not getting executed at the defined time.

I have used apscheduler to schedule an interval job to execute a python script every 10 minutes and its running successfully, but cron job is where i am struggling.

A sample code for cron job which was scheduled to be run 2 pm today -

from apscheduler.schedulers.blocking import BlockingScheduler

def cron_process():
    print ("periodic print")

scheduler = BlockingScheduler()
scheduler.add_job(process, 'cron', day_of_week = 'sun', hour=14)
scheduler.start()

A sample code for interval job which is running successfully every 10 minutes when execution is initiated -

def interval_process():
     print ("print every 10 minutes")

scheduler = BlockingScheduler()
scheduler.add_job(process, 'interval', minutes=10)
scheduler.start()

Expected result is that the cron job is getting executed at the defined time, on the same lines of the interval job.

Please advise where am i making mistake or what else i am missing in the code.

Thanks.


Solution

  • A slightly modified version of your code is working for me (I adjusted the cron entry so I wouldn't have to wait a week to see the results, and I made the function name argument match):

    #!/usr/bin/env python3
    from apscheduler.schedulers.blocking import BlockingScheduler
    
    def cron_process():
        print ('periodic print')
    
    scheduler = BlockingScheduler()
    scheduler.add_job(cron_process, 'cron', day_of_week = 'mon', hour='*', minute='*')
    scheduler.start()