liveBook · Manning

Crontab format

As explained in Kubernetes in Action, Second Edition

The schedule field specifies the recurring time expression that determines when a CronJob creates Jobs.

Crontab format is the five-field schedule expression used by a CronJob to decide when the CronJob controller creates Jobs. The fields represent minute, hour, day of month, month, and day of week, from left to right. section 18.2 section 18.2.2

Build a schedule by replacing asterisks with values, ranges, lists, or step patterns:

  • `* * * * *` runs every minute; `15 * * * *` runs 15 minutes after every hour. section 18.2.2
  • `1-5` selects an inclusive range, `1,2,5-8` selects values and ranges, and `*/3` selects every third value. section 18.2.2
  • When both day-of-month and day-of-week contain restrictions, either matching field can trigger the schedule; `* * 13 * 5` runs on every 13th and every Friday. section 18.2.2
  • Use `@hourly`, `@daily`, `@weekly`, `@monthly`, `@yearly`, or `@annually` for common recurring schedules. section 18.2.2

Run a Job every minute

Set `spec.schedule` to `"* * * * *"` in a `batch/v1` CronJob. The first Job appears when the next minute starts, and `kubectl get cj` reports the schedule and last scheduling time. section 18.2.1

Use a specific time zone

Combine `schedule: "0 3 * * *"` with `timeZone: CET` to schedule the CronJob at 03:00 in the specified time zone. section 18.2.2

Listing 18.17 A CronJob that runs a Job every minute
apiVersion: batch/v1       #1
kind: CronJob             #1
metadata:
  name: aggregate-responses-every-minute
spec:
  schedule: "* * * * *"      #2
  jobTemplate:                                                                #3
    metadata:                                                                 #3
      labels:                                                                 #3
        app: aggregate-responses-today                                        #3
    spec:                                                                     #3
      template:                                                               #3
        metadata:                                                             #3
          labels:                                                             #3
            app: aggregate-responses-today                                    #3
        spec:                                                                 #3
          restartPolicy: OnFailure                                            #3
          containers:                                                         #3
          - name: updater                                                     #3
            image: mongo:5                                                    #3
            command:                                                          #3
            - mongosh                                                         #3
            - mongodb+srv://quiz-pods.kiada.svc.cluster.local/kiada?tls=false #3
            - --quiet                                                         #3
            - --file                                                          #3
            - /script.js                                                      #3
            volumeMounts:                                                     #3
            - name: script                                                    #3
              subPath: script.js                                              #3
              mountPath: /script.js                                           #3
          volumes:                                                            #3
          - name: script                                                      #3
            configMap:                                                        #3
              name: aggregate-responses-today                                 #3

Inspect the complete CronJob manifest, including the schedule field and Job template. section 18.2.1

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains five fields, values, ranges, lists, steps, shortcuts, matching rules, and time-zone configuration without controller internals.
Examples
Uses every-minute, weekday, date-specific, interval, special-value, and time-zone schedule examples.
What it assumes
Assumes familiarity with CronJobs and Job templates while introducing crontab syntax from its five basic fields.
What you can do after
Operate/configure CronJobs by writing schedule fields and checking created Jobs with kubectl.
How it connects
Connects crontab expressions to CronJobs, Jobs, controllers, and time zones.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.