5

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.

Nakul Sharma
  • 143
  • 2
  • 9

2 Answers2

7

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()
davejagoda
  • 2,420
  • 1
  • 20
  • 27
0

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)
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
  • What should I do if I wanted to run in 12:00 and 17:00 ? – alper Aug 01 '21 at 14:29
  • @`alper`: [commit](https://raw.githubusercontent.com/MilovanTomasevic/stackoverflow/main/sched/multiple_times.py) for you :) – Milovan Tomašević Aug 01 '21 at 22:01
  • As I understand we can just do: `scheduler.add_job(self.send_message, "cron", hour="12,17")`? Like having hours seperating them with comma – alper Aug 02 '21 at 13:21
  • The syntax is as I already wrote, I think there is really nothing that is unclear. I have especially written an example for you case if it is not clear to you, please take and read the great [official documentation](https://apscheduler.readthedocs.io/en/stable/) in which everything is really explained. Good luck – Milovan Tomašević Aug 02 '21 at 22:31