Links

Get next occurrences of a cron job

Tags: #python #cron #croniter #job #occurrences #scheduling
Author: Florent Ravenel
Last update: 2023-05-09 (Created: 2023-05-09)
Description: This notebook will show how to get the next x occurrences of your cron job using croniter. It is usefull for organizations to schedule tasks and jobs.
References:

Input

Import libraries

try:
import croniter
except:
!pip install croniter --user
import croniter
import pytz
from datetime import datetime

Setup Variables

  • cron_expression: cron expression to be used
  • occurrences: number of occurrences to be returned
  • timezone: timezone identifier
cron_expression = "*/15 * * * *"
occurrences = 5
timezone = 'Europe/Paris'

Model

Set the timezone

# Set the timezone
tz = pytz.timezone(timezone)

Create a cron iterator

# Create a cron iterator with the timezone
cron = croniter.croniter(cron_expression, datetime.now(tz), tz)

Output

Get the next x occurrences of the cron job and convert them to text

for i in range(occurrences):
next_occurrence = cron.get_next(datetime)
print(next_occurrence.strftime('%A, %B %d, %Y at %I:%M %p'))