11

Here is part of my CronJob spec:

kind: CronJob
spec:
    schedule: #{service.schedule}

For a specific environment a cron job is set up, but I never want it to run. Can I write some value into schedule: that will cause it to never run?

I haven't found any documentation for all supported syntax, but I am hoping for something like:

@never or @InABillionYears

user1283776
  • 19,640
  • 49
  • 136
  • 276
  • Possible duplicate of [Disabling cronjob in Kubernetes](https://stackoverflow.com/questions/52776690/disabling-cronjob-in-kubernetes) – switchboard.op Sep 16 '19 at 15:26

4 Answers4

24

@reboot doesn't guarantee that the job will never be run. It will actually be run always when your system is booted/rebooted and it may happen. It will be also run each time when cron daemon is restarted so you need to rely on that "typically it should not happen" on your system...

There are far more certain ways to ensure that a CronJob will never be run:

  1. On Kubernetes level by suspending a job by setting its .spec.suspend field to true

You can easily set it using patch:

kubectl patch cronjobs <job-name> -p '{"spec" : {"suspend" : true }}'
  1. On Cron level. Use a trick based on fact that crontab syntax is not strictly validated and set a date that you can be sure will never happen like 31th of February. Cron will accept that as it doesn't check day of the month in relation to value set in a month field. It just requires that you put valid numbers in both fields (1-31 and 1-12 respectively). You can set it to something like:

* * 31 2 *

which for Cron is perfectly valid value but we know that such a date is impossible and it will never happen.

mario
  • 9,858
  • 1
  • 26
  • 42
  • 3
    Dont do this (February 31) in google kubernetes engine. It will try starting the cron every minute or so, and complain about the schedule. use the "Suspend Job" native functionality and just run it manually from gcloud/console – Nth.gol Nov 29 '19 at 16:27
21
kind: CronJob
spec:
    suspend: true
switchboard.op
  • 1,878
  • 7
  • 26
  • 4
    This was already answered in a much more complete answer before...Also, keep in mind that code-only answers are generally frowned upon on this site. Answers are supposed to explain things like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Sep 16 '19 at 20:44
  • 6
    Fair enough, Eduardo. Speaking from personal experience however, when I wanted to know how to stop a cron job from running, all I wanted was `spec.suspend=true`. – switchboard.op Sep 16 '19 at 21:07
0

Why do you need this to be a CronJob in the first place? If you never want it to run, you could specify a simple Job: https://kubernetes.io/docs/concepts/workloads/controllers/job/

  • I, personally, like the idea of saving the spec in kubernetes and not having to pass a yaml/json/etc... file every time I want it to run. Is this possible using JUST a `Job`? – JonTroncoso Jul 01 '21 at 05:55
  • 1
    a job will still try and run to successful completion – majita Jan 06 '22 at 12:34
  • 3
    Yes. One use case I though of for creating a CronJob and scheduling it to never run was to then use `--from=cronjob` to on demand create a Job. For this use case you can rather just create the Job without the intermediate step. – Sebastian Hätälä Jan 07 '22 at 11:01
  • 2
    @SebastianHätälä As majita said, if you directly create the job, it will be run instantly. However, as JonTroncoso said, there might be cases where you just want to publish a "job template", but not run it immediately and instead run it on demand with `--from=cronjob`. Of course you don't _need_ the cronjob for that, but it's convenient to already have it in the cluster. – Gerrit-K Oct 17 '22 at 14:01
-3

I think you can use @reboot,

see: https://en.wikipedia.org/wiki/Cron

@reboot configures a job to run once when the daemon is started. Since cron is typically never restarted, this typically corresponds to the machine being booted.

user1283776
  • 19,640
  • 49
  • 136
  • 276