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

2

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.

Mandell answered 17/2, 2019 at 21:0 Comment(0)
A
8

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()
Anceline answered 18/2, 2019 at 23:44 Comment(2)
This code runs the job once every minute but only on Mondays. Is this really what you wanted?Curtal
This example was the exactly what I wanted! Worked excellent! Thanks!Treacherous
L
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)
Lottielotto answered 8/10, 2020 at 20:21 Comment(4)
What should I do if I wanted to run in 12:00 and 17:00 ?Upi
@alper: commit for you :)Irish
As I understand we can just do: scheduler.add_job(self.send_message, "cron", hour="12,17")? Like having hours seperating them with commaUpi
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 in which everything is really explained. Good luckIrish

© 2022 - 2025 — McMap. All rights reserved.