4
votes

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.

2

2 Answers

5
votes

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()
0
votes

Official example with logger: for more information, you can see the official document. Current version: APScheduler==3.6.3

install/requirement:

pip install APScheduler

Example:

from apscheduler.schedulers.blocking import BlockingScheduler
import logging
import sys

logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
sh.setFormatter(formatter)
logger.addHandler(sh)


def job_function():
    print("Hello World")

sched = BlockingScheduler()

# Schedules job_function to be run on the third Friday
# of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00
sched.add_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')

sched.start()

output:

[Thu, 08 Oct 2020 22:09:41] INFO [base.py.add_job:440] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[Thu, 08 Oct 2020 22:09:41] INFO [base.py._real_add_job:881] Added job "job_function" to job store "default"
[Thu, 08 Oct 2020 22:09:41] INFO [base.py.start:166] Scheduler started
[Thu, 08 Oct 2020 22:09:41] DEBUG [base.py._process_jobs:940] Looking for jobs to run
[Thu, 08 Oct 2020 22:09:41] DEBUG [base.py._process_jobs:1019] Next wakeup is due at 2020-11-20 00:00:00+01:00 (in 3639018.401552 seconds)