Skip to main content

Working with CronJobs

1. Creating CronJob

Field Description Example
metadata.name Name of the CronJob my-cronjob
spec.schedule Cron schedule expression (standard format) 0 0 * * *
spec.jobTemplate Template for the Job to be created
{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "my-container",
            "image": "alpine:latest",
            "command": ["echo", "Hello World"]
          }
        ],
        "restartPolicy": "OnFailure"
      }
    }
  }
}
spec.startingDeadlineSeconds Deadline in seconds for starting a job if it misses its scheduled time 300
spec.concurrencyPolicy Policy for handling concurrent executions (Allow, Forbid, Replace) Forbid
spec.suspend Pause the CronJob if set to true false

Example: Creating a CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello-cronjob
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello-container
            image: alpine:latest
            command:
            - echo
            - "Hello World"
          restartPolicy: OnFailure
Note: The schedule uses the standard cron format with five fields (minute, hour, day of month, month, day of week). The jobTemplate defines the Job that will be created when the CronJob runs.

2. Listing CronJobs

Field Description Example Value
metadata.name Name of the CronJob my-cronjob
spec.schedule Cron schedule expression 0 0 * * *
spec.jobTemplate.spec.template.spec.containers.image Container image used for the job nginx:1.21
status.lastScheduleTime Timestamp of last scheduled run 2024-05-10T12:00:00Z

Example: List CronJobs in a Namespace

kubectl get cronjobs -n my-namespace

Example: List All CronJobs Across All Namespaces

kubectl get cronjobs --all-namespaces

3. Describing CronJob Details

Field Description Example Value
metadata.name Name of the CronJob my-cronjob
metadata.namespace Namespace where the CronJob is defined default
spec.schedule Cron schedule expression (standard cron format) 0 0 * * *
spec.jobTemplate Template for the Job to be created
{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "job-container",
            "image": "busybox:1.35"
          }
        ],
        "restartPolicy": "OnFailure"
      }
    }
  }
}
spec.startingDeadlineSeconds Deadline in seconds for starting a job if it misses its scheduled time 300
spec.concurrencyPolicy Policy for handling concurrent executions (Allow, Forbid, Replace) Allow
spec.successfulJobsHistoryLimit Number of successful jobs to retain 3
spec.failedJobsHistoryLimit Number of failed jobs to retain 1
spec.suspend Pause execution of the CronJob if set to true false

Example: CronJob Spec

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-cronjob
  namespace: default
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: job-container
            image: busybox:1.35
          restartPolicy: OnFailure
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1

4. Setting Schedule Expression

Field Value Description
spec.schedule "0 0 * * *" Cron schedule expression in UTC

Example: Cron Schedule Expression

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello-cronjob
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command:
            - /bin/sh
            - -c
            - echo "Hello, Kubernetes!"
          restartPolicy: OnFailure

5. Understanding Cron Syntax

Field Allowed Values Description
Minute 0-59 Minute of the hour (0-59)
Hour 0-23 Hour of the day (0-23)
Day of Month 1-31 Day of the month (1-31)
Month 1-12 or * (wildcard) Month of the year (1-12 or *)
Day of Week 0-6 or * (wildcard) Day of the week (0-6, 0 = Sunday or *)

Example: Cron Schedule Expression


* * * * * 
Note: The five fields represent minute, hour, day of month, month, and day of week respectively.

6. Configuring Concurrency Policy

Policy Description Behavior
Allow Allows multiple jobs to run concurrently Multiple instances can start at the same time
Forbid Prevents concurrent job runs Waits for the previous job to complete before starting a new one
Replace Replaces the currently running job with a new one Terminates the current job and starts a new one immediately

Example: Setting Concurrency Policy in CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "0 0 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: example
            image: busybox
            command:
            - echo
            - "Hello from CronJob"
          restartPolicy: Never

7. Setting Successful Jobs History

Field Description Default Value Notes
successfulJobsHistoryLimit Number of successful jobs to retain 3 Controls how many completed CronJobs are kept in the history

Example: Setting Successful Jobs History Limit

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "0 0 * * *"
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command:
            - echo
            - "Hello from CronJob"
          restartPolicy: OnFailure

8. Setting Failed Jobs History

Field Description Default Value Notes
failedJobsHistoryLimit Number of failed jobs to retain 1 Controls how many failed CronJobs are kept in the history. Setting to null means no limit.

Example: Setting Failed Jobs History Limit

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "0 0 * * *"
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command:
            - /bin/sh
            - -c
            - echo "Hello from CronJob"
          restartPolicy: OnFailure

9. Suspending CronJobs

Property Description Values
suspend Controls whether the CronJob is suspended or not true or false

Example: Suspending a CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "0 0 * * *"
  suspend: true
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-container
            image: busybox
            command:
            - echo
            - "Hello from CronJob"
          restartPolicy: OnFailure
Note: Setting suspend: true prevents the CronJob from creating new Jobs, but does not affect already running Jobs.

10. Setting Starting Deadline

Field Description Default Value Notes
startingDeadlineSeconds Specifies the duration (in seconds) within which the CronJob must create a Pod. null If the CronJob misses its scheduled time and this value is set, it will not run if the deadline has passed.

Example: CronJob with startingDeadlineSeconds

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "0 0 * * *"
  startingDeadlineSeconds: 300
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command:
            - echo
            - "Hello from CronJob"
          restartPolicy: OnFailure
Note: If startingDeadlineSeconds is set to null, the CronJob will not enforce any deadline and will attempt to run even if it misses its scheduled time.

11. Creating Manual Job Run

Field Description Example
apiVersion API version of the CronJob resource batch/v1
kind Kind of the resource (must be CronJob) CronJob
metadata.name Name of the CronJob my-cronjob
spec.schedule Cron schedule expression (e.g., every day at 2 AM) 0 2 * * *
spec.jobTemplate.spec.template.spec.containers Container spec to run when the job executes [{ name: "job-container", image: "alpine:latest", command: ["echo", "Hello World"] }]

Example: Creating a CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: job-container
            image: alpine:latest
            command: ["echo", "Hello World"]
          restartPolicy: OnFailure
Note: The jobTemplate defines the structure of the Job that will be created when the CronJob runs. The schedule field uses a standard cron expression format.

Example: Manually Triggering a CronJob

kubectl create job --from=cronjob/my-cronjob manual-job-run
Note: This command creates a new Job based on the CronJob's template. The job will run once immediately and then follow its own lifecycle independently of the CronJob.

12. Deleting CronJobs

Property Description Values
cronjob.name Name of the CronJob to delete my-cronjob
cronjob.namespace Namespace where the CronJob exists default

Example: Delete a CronJob

kubectl delete cronjob my-cronjob -n default
Note: Deleting a CronJob stops all future scheduled jobs, but does not delete any pods that were already created by the CronJob.

Was this article helpful?