128

I have scheduled an application to run as a CronJob in Kubernetes. When there is a code change, I'm also changing the image of the CronJob.

I'm looking for an option where I can disable the currently running CronJob and deploy a new CronJob with the latest image version.

How can I disable a CronJob in Kubernetes without deleting its Deployment?

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Jasmitha Meka
  • 1,387
  • 3
  • 10
  • 11
  • Kubernetes docs for cronjob [schedule suspension](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#schedule-suspension) – tread May 31 '23 at 13:34

7 Answers7

228

If you want to suspend cronjob via patch, use:

kubectl patch cronjobs <job-name> -p '{"spec" : {"suspend" : true }}'
aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
Amityo
  • 5,635
  • 4
  • 22
  • 29
  • 3
    I am trying to suspend using above command but getting error error: unable to parse "'{spec": yaml: found unexpected end of stream – lazydeveloper Aug 05 '19 at 14:48
  • 5
    `kubectl patch cronjobs -p '{\"spec\" : {\"suspend\" : true }}'` – idubnori Jan 22 '20 at 04:37
  • @Zhongxia Zhou patch with suspend false – Amityo Jun 15 '20 at 17:52
  • 8
    To suspend all the cronjobs at one go `kubectl get cronjobs | grep False | cut -d' ' -f 1 | xargs kubectl patch cronjobs -p '{"spec" : {"suspend" : true }}'` – Subbu Sep 10 '20 at 09:39
  • I can't make it work. I get the following error: "error: unable to parse "'{\"spec\"": yaml: found unexpected end of stream". Am I supposed to change this command in some way (apart from replacing with the name of the cronjob)? – edn Dec 27 '21 at 21:35
  • @idubnori, there's no need to escape double quotes when wrapping the whole payload in single quotes. – Victor Schröder Apr 05 '22 at 17:22
  • @Subbu not sure if the best solution but it works for me on Windows, I had the same issue. As you see it seems the that there is a quote that seems to be automatically added, which brakes the json parsing. I used the following command `kubectl patch cronjobs -p {\"spec\":{\"suspend\":true}}` by escaping the quotes and removing the whitespaces. Looks like a terminal problem, but to time to search for the proper solution now – user2944582 Apr 26 '23 at 08:17
26
kubectl patch cronjobs job-name -p '{"spec" : {"suspend" : true }}'
aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
25

Edit your current cronjob resource to include the .spec.suspend field and set it to true. Any currently running jobs will complete but future jobs will be suspended.

If you also need to stop currently running jobs, you'll have to delete them

Patrick W
  • 4,603
  • 1
  • 12
  • 26
19

You can use something which will be valid with respect to Cron Job format but actually that date should not appear anytime in calendar date like 31 Feb.

* * 31 2 *
PRADEEP PANDEY
  • 347
  • 1
  • 2
6

Option 1 with command line

$ kubectl patch cronjobs $(kubectl get cronjobs | awk '{ print $1 }' | tail -n +2) -p '{"spec" : {"suspend" : true }}'

Option 2 with command line:

$ kubectl get cronjobs | grep False | cut -d' ' -f 1 | xargs kubectl patch cronjobs -p '{"spec" : {"suspend" : true }}'

Option 3 creating resource quotas. I believe that is the cleaner option.

cat <<EOF | kubectl apply -f -
# https://kubernetes.io/docs/concepts/policy/resource-quotas/#object-count-quota
apiVersion: v1
kind: ResourceQuota
metadata:
  name: limit-generic-resources
spec:
  hard:
    pods: "0"
    count/persistentvolumeclaims : "0"
    count/services : "0"
    count/secrets : "0"
    count/configmaps : "0"
    count/replicationcontrollers : "0"
    count/deployments.apps : "0"
    count/replicasets.apps : "0"
    count/statefulsets.apps : "0"
    count/jobs.batch : "0"
    count/cronjobs.batch : "0"
EOF
Marcos Aranda
  • 61
  • 1
  • 2
2

Here's arguably the simplest way you can patch multiple CronJobs (and other patch-able objects like Deployments or Pods):

kubectl patch $(kubectl get cronjob -o name | grep my-filter) -p '{"spec" : {"suspend" : true }}'

Notice the use of -o name which simplifies getting a list of objects (here: CronJobs) names to process (without the need to parse a table with awk).

You can patch all of them at once or just a subset of names restricted to those meeting filtering criteria (here: containing my-filter).

mirekphd
  • 4,799
  • 3
  • 38
  • 59
1

You can either use patch command like below -

kubectl patch cronjobs <cron-jon-name> -n <namespace> -p '{"spec" : {"suspend" : true }}'

or you can first get the cronjob resource yaml - kubectl get cronjobs <cron-jon-name> -n <namespace> -o yaml > cronjob.yaml

Edit yaml file under spec section with suspend to true.

Sapna
  • 635
  • 9
  • 18