Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engmsaleh/b4f049eb0514bebb66f21d04774a13e8 to your computer and use it in GitHub Desktop.
Save engmsaleh/b4f049eb0514bebb66f21d04774a13e8 to your computer and use it in GitHub Desktop.
How to trigger a Cron Job manually on Kubernetes

Triggering a Cron Job manually on Kubernetes

So you've defined a cronjob.yaml manifest, but it doesn't run until midnight and you want to test it now? Simply replace the variables, and run the following command to create a job from the cronjob.

kubectl create job --from=cronjob/{{ cron_job_name }} {{ the-name-of-this-one-off-job }}

This will create a one-off job in your cluster based on your cronjob.yaml manifest, which might look something like this.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ cron_job_name }}
spec:
  schedule: "0 1 * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: curl
            image: buildpack-deps:curl
            args:
            - /bin/sh
            - -ec
            - curl https://example.com
          restartPolicy: Never

Checking The Status

To check the status, use the following commands to investigate.

kubectl get jobs --selector=job-name={{ the-name-of-this-one-off-job }}
kubectl get po --selector=job-name={{ the-name-of-this-one-off-job }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment