⎈ k8s knowledge compiler

CronJob [page]deterministic

A CronJob starts one-time Jobs on a repeating schedule.

concepts

A _CronJob_ creates [Jobs](#gloss:job) on a repeating schedule.

CronJob is meant for performing regular scheduled actions such as backups, report generation, and so on. One CronJob object is like one line of a _crontab_ (cron table) file on a Unix system. It runs a Job periodically on a given schedule, written in [Cron](https://en.wikipedia.org/wiki/Cron) format.

CronJobs have limitations and idiosyncrasies. For example, in certain circumstances, a single CronJob can create multiple concurrent Jobs. See the [limitations](#cron-job-limitations) below.

When the control plane creates new Jobs and (indirectly) Pods for a CronJob, the `.metadata.name` of the CronJob is part of the basis for naming those Pods. The name of a CronJob must be a valid [DNS subdomain](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names) value, but this can produce unexpected results for the Pod hostnames. For best compatibility, the name should follow the more restrictive rules for a [DNS label](/docs/concepts/overview/working-with-objects/names#dns-label-names). Even when the name is a DNS subdomain, the name must be no longer than 52 characters. This is because the CronJob controller will automatically append 11 characters to the name you provide and there is a constraint that the length of a Job name is no more than 63 characters.

## Example

This example CronJob manifest prints the current time and a hello message every minute:

([Running Automated Tasks with a CronJob](/docs/tasks/job/automated-tasks-with-cron-jobs/) takes you through this example in more detail).

## Writing a CronJob spec ### Schedule syntax The `.spec.schedule` field is required. The value of that field follows the [Cron](https://en.wikipedia.org/wiki/Cron) syntax:

``` # ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of the month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday) # │ │ │ │ │ OR sun, mon, tue, wed, thu, fri, sat # │ │ │ │ │ # │ │ │ │ │ # * * * * * ```

For example, `0 3 * * 1` means this task is scheduled to run weekly on a Monday at 3 AM.

The format also includes extended "Vixie cron" step values. As explained in the [FreeBSD manual](https://www.freebsd.org/cgi/man.cgi?crontab%285%29):

> Step values can be used in conjunction with ranges. Following a range > with `/<number>` specifies skips of the number's value through the > range. For example, `0-23/2` can be used in the hours field to specify > command execution every other hour (the alternative in the V7 standard is > `0,2,4,6,8,10,12,14,16,18,20,22`). Steps are also permitted after an > asterisk, so if you want to say "every two hours", just use `*/2`.

> Note: A question mark (`?`) in the schedule has the same meaning as an asterisk `*`, that is, it stands for any of available value for a given field.

Other than the standard syntax, some macros like `@monthly` can also be used:

| Entry | Description | Equivalent to | | ------------- | ------------- |------------- | | @yearly (or @annually) | Run once a year at midnight of 1 January | 0 0 1 1 * | | @monthly | Run once a month at midnight of the first day of the month | 0 0 1 * * | | @weekly | Run once a week at midnight on Sunday morning | 0 0 * * 0 | | @daily (or @midnight) | Run once a day at midnight | 0 0 * * * | | @hourly | Run once an hour at the beginning of the hour | 0 * * * * |

To generate CronJob schedule expressions, you can also use web tools like [crontab.guru](https://crontab.guru/).

### Job template

The `.spec.jobTemplate` defines a template for the Jobs that the CronJob creates, and it is required. It has exactly the same schema as a [Job](/docs/concepts/workloads/controllers/job/), except that it is nested and does …(trimmed)

Sources

concepts/workloads/controllers/cron-jobs.md · docCronJob

Related (20)

references JobJobs conf=1
references Labellabels conf=1
references Annotationannotations conf=1
references kube-controller-managerkube-controller-manager conf=1
references Controllercontroller conf=1
part_of Exampledescribes conf=1
part_of Writing a CronJob specdescribes conf=1
part_of CronJob limitations {#cron-job-limitations}describes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Schedule syntaxdescribes conf=1
part_of Job templatedescribes conf=1
part_of Concurrency policydescribes conf=1
part_of Schedule suspensiondescribes conf=1
part_of Jobs history limitsdescribes conf=1
part_of Time zonesdescribes conf=1
part_of Unsupported TimeZone specificationdescribes conf=1
part_of Modifying a CronJobdescribes conf=1
part_of Job creationdescribes conf=1
api_for CronJobdocuments API object conf=1

← all Docs