Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule cron job to never happen?

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

like image 632
user1283776 Avatar asked Sep 16 '19 09:09

user1283776


2 Answers

@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.

like image 126
mario Avatar answered Oct 28 '22 08:10

mario


kind: CronJob
spec:
    suspend: true
like image 27
switchboard.op Avatar answered Oct 28 '22 10:10

switchboard.op