Skip to main content

Add a New CronJob

Option 1: Create a CronJob Using a YAML Manifest

1. Create a new cronjob.yaml file:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "*/5 * * * *"  # Runs every 5 minutes
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: example
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes CronJob!
          restartPolicy: OnFailure

2. Apply the new CronJob:

kubectl apply -f cronjob.yaml

Option 2: Use kubectl create

You can create a CronJob directly with the kubectl create command:

kubectl create cronjob <cronjob-name> \
  --schedule="*/5 * * * *" \
  --image=busybox \
  -- /bin/sh -c "date; echo Hello from the Kubernetes CronJob!"

This creates a simple CronJob that runs every 5 minutes and prints the current date and a message.

Verifying Changes

  • List CronJobs to ensure the changes or new CronJob were applied:
kubectl get cronjobs
  • Describe CronJob to see detailed information:
kubectl describe cronjob <cronjob-name>

Let me know if you need help with any specific part!