Suspend All Kubernetes Cronjobs

Max Rosin

Recently, I had the need to suspend all Kubernetes cronjobs in a cluster. Looking around for an existing one-liner I came across this helpful gist. It loops over all namespaces, gets all cronjobs in each and patches them to spec.suspend: true.

for ns in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}"); do
  for cj in $(kubectl get cronjobs -n "$ns" -o name); do
    kubectl patch "$cj" -n "$ns" -p '{"spec" : {"suspend" : true }}';
  done
done

And to enable them again, we can patch them to spec.suspend: false.

for ns in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}"); do
  for cj in $(kubectl get cronjobs -n "$ns" -o name); do
    kubectl patch "$cj" -n "$ns" -p '{"spec" : {"suspend" : false }}';
  done
done

Keep in mind that resuming overdue cronjobs immediately schedules new jobs, so depending on your jobs this might cause some additional load.

Max Rosin

I am a Systems Engineer from Germany. In my personal time I like to tinker with all kinds of technologies, some of it is available here at my blog. In my professional life I work a lot with Kubernetes and monitoring systems. Occasionally I give workshops about Kubernetes and conference talks about my work.
If you want to talk about one of my posts, feel free to reach out via Twitter.