Schedule cron job to never happen?

9/16/2019

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
cron
kubernetes

3 Answers

9/16/2019

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
Source: StackOverflow

9/16/2019

@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
Source: StackOverflow

9/16/2019
kind: CronJob
spec:
    suspend: true
-- switchboard.op
Source: StackOverflow