Skip to main content

crontab

A Crontab can be used as the run_every value of a periodic task entry to add :manpage:crontab(5)-like scheduling.

Attributes

AttributeTypeDescription
minuteset = *A (list of) integers from 0-59 or a string representing a Crontab pattern that represent the minutes of an hour of when execution should occur.
hourset = *A (list of) integers from 0-23 or a string representing a Crontab pattern that represent the hours of a day of when execution should occur.
day_of_weekset = *A (list of) integers from 0-6 (Sunday=0) or a string representing a Crontab pattern that represent the days of a week that execution should occur.
day_of_monthset = *A (list of) integers from 1-31 or a string representing a Crontab pattern that represents the days of the month that execution should occur.
month_of_yearset = *A (list of) integers from 1-12 or a string representing a Crontab pattern that represents the months of the year during which execution can occur.
nowfuncallableFunction returning the current date and time (datetime.datetime).
app[Celery](../app/base/celery.md?sid=celery_app_base_celery)The Celery app instance.

Constructor

Signature

def crontab(
minute: Cronspec = '*',
hour: Cronspec = '*',
day_of_week: Cronspec = '*',
day_of_month: Cronspec = '*',
month_of_year: Cronspec = '*',
**kwargs: Any
) - > None

Parameters

NameTypeDescription
minuteCronspec = '*'Minute(s) of an hour when the task should run (0-59).
hourCronspec = '*'Hour(s) of a day when the task should run (0-23).
day_of_weekCronspec = '*'Day(s) of the week when the task should run (0-6, Sunday=0).
day_of_monthCronspec = '*'Day(s) of the month when the task should run (1-31).
month_of_yearCronspec = '*'Month(s) of the year when the task should run (1-12).
**kwargsAnyAdditional keyword arguments passed to the BaseSchedule parent class.

Methods


from_string()

@classmethod
def from_string(
crontab: str
) - > [crontab](crontab.md?sid=celery_schedules_crontab)

Create a Crontab from a cron expression string. For example crontab.from_string('* * * * *').

Parameters

NameTypeDescription
crontabstrA space-separated string containing five cron fields: minute, hour, day of month, month, and day of week

Returns

TypeDescription
[crontab](crontab.md?sid=celery_schedules_crontab)A new crontab instance initialized with the parsed schedule components

remaining_delta()

@classmethod
def remaining_delta(
last_run_at: datetime,
tz: tzinfo | None,
ffwd: type
) - > tuple[datetime, Any, datetime]

Calculates the time difference between the last run and the next scheduled execution based on the crontab rules.

Parameters

NameTypeDescription
last_run_atdatetimeThe timestamp when the task was last executed
tz`tzinfoNone`
ffwdtypeThe fast-forward utility class used to calculate time jumps

Returns

TypeDescription
tuple[datetime, Any, datetime]A tuple containing the localized last run time, the fast-forward delta to the next run, and the localized current time

remaining_estimate()

@classmethod
def remaining_estimate(
last_run_at: datetime,
ffwd: type
) - > timedelta

Estimate of next run time. Returns when the periodic task should run next as a :class:~datetime.timedelta.

Parameters

NameTypeDescription
last_run_atdatetimeThe timestamp of the last execution to calculate the next interval from
ffwdtypeThe fast-forward utility class used for time calculations

Returns

TypeDescription
timedeltaThe duration of time remaining until the next scheduled execution

is_due()

@classmethod
def is_due(
last_run_at: datetime
) - > tuple[bool, datetime]

Return tuple of (is_due, next_time_to_run). If :setting:beat_cron_starting_deadline has been specified, the scheduler will make sure that the last_run_at time is within the deadline.

Parameters

NameTypeDescription
last_run_atdatetimeThe timestamp of the last successful execution to check against the schedule and deadline

Returns

TypeDescription
tuple[bool, datetime]A tuple where the first element indicates if the task is ready to run, and the second is the time remaining until the next run