Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 2.19 KB

Python_Get_next_occurrences_of_a_cron_job.md

File metadata and controls

69 lines (45 loc) · 2.19 KB



Template request | Bug report | Generate Data Product

Tags: #python #cron #croniter #job #occurrences #scheduling

Author: Florent Ravenel

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'))