117

Is there any way to create a cron expression for not running the job at all?

I though of using this expression :

0 0 0 1 1 ? 3099

...which will practically do the job as it will run on year 3099, but is there a cleaner way to do it?

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Madhu CM
  • 2,296
  • 6
  • 29
  • 38

6 Answers6

140

If you're still looking for something robust even in the far future, try https://stackoverflow.com/a/13938099/1601531, where I suggest the use of February 31st in crontab entries which are never intended to execute.

0 0 5 31 2 ?
Community
  • 1
  • 1
Eric Tjossem
  • 2,536
  • 1
  • 15
  • 18
  • 7
    on spring boot's cron interpreter this causes `Invalid cron expression "0 0 5 31 2 *" led to runaway search for next trigger` – nurettin Dec 13 '18 at 13:06
  • 2
    did not work for me but 29 Feb worked "0 0 0 29 2 ?" – vsingh Mar 05 '19 at 16:38
  • 74
    You do know that 29 February will execute some years, right? – Magnus H Aug 05 '19 at 11:30
  • 16
    Using spring boot, you can disable cron jobs with a dash "-" , https://stackoverflow.com/questions/36403671/spring-cron-scheduler-disable-pattern – Lubco Feb 03 '20 at 08:37
  • 3
    `Microsoft.Azure.WebJobs.Extensions: The schedule expression '0 0 5 31 2 ?' was not recognized as a valid cron expression or timespan string.` – Ruslan Nov 13 '20 at 10:57
  • 1
    If you are using Spring Framework with Quartz Scheduler, the above crontab expression will result in an IllegalArgumentExpression with the following error message: "Invalid cron expression "0 0 5 31 2 ?" led to runaway search for next trigger". (Experienced this using Spring 4.1.2.) While this answer may actually work with crontab, it may not work with stuff mimicking crontab. – Attila Csipak Dec 07 '20 at 10:02
  • @Lubco `-` only works with `@Scheduled`. For example, spring's `CronExpression.parse( "-" )` throws exception. – cdalxndr Apr 21 '21 at 07:21
  • Works with spring boot 5.3.5 cron interpreter: `CronExpression.parse("0 0 5 31 2 ?")`. Getting next run date returns `null` so it prevents running in schedulers. – cdalxndr Apr 21 '21 at 07:35
  • It looks like the values are swapped because of spring, I think it should be [`0 5 31 2 *`](https://crontab.guru/#0_5_31_2_0) to have February 31st. – A.L Jun 30 '22 at 13:33
31

I needed a valid cron schedule (? syntax not working in my system) which resolves real dates, but to be effectively "never". My current best solution was to pick the most recent leap year and see what day Feb 29th fell on. Feb 29 2016 was a Monday, so the next Monday Feb 29 is currently furthest away.

0 0 29 2 1 yields the next 5 triggers as:

02/29/2044 00:00:00Z
02/29/2072 00:00:00Z
02/29/2112 00:00:00Z
02/29/2140 00:00:00Z
02/29/2168 00:00:00Z

Not perfect but it'll do.

Tim Iles
  • 2,232
  • 2
  • 24
  • 26
  • 47
    Please don't do this. It's how we ended up with Y2K bug. – Madbreaks Dec 16 '19 at 19:06
  • 9
    Agree that this approach shouldn't be used in a production environment - for my purpose, production used a normal schedule, but dev/test environments used this hack so that the code never actually triggered. Though I think your comparison to the Y2K bug is a bit overly dramatic, no? – Tim Iles Dec 16 '19 at 23:16
  • Fair enough but I think folks without that insight coming to SO for guidance might not understand why or that it shouldn't be used in production. As for Y2K...it was caused by one or more developers thinking Y2K was so far in the future that they needn't worry about it "for now". They probably didn't, either, but we did. ;) – Madbreaks Dec 17 '19 at 17:54
  • 1
    For the Spring schedular this causes the `Invalid cron expression "0 0 3 29 2 1" led to runaway search for next trigger` error message. A `?` for the last item is accepted though (but will run once every 4 years). – ᴠɪɴᴄᴇɴᴛ Oct 08 '20 at 12:30
  • @Madbreaks What about Y10K, you know, when we get to 5 digit years. I am not sure this is really a Y2K style bug. – Jeremy Jul 12 '21 at 16:08
  • THIS DOES NOT WORK. Following Cron's Wikipedia article (that refer to https://pubs.opengroup.org/onlinepubs/007904975/utilities/crontab.html): "While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" (field 3) and "day of week" (field 5) are restricted (not "*"), then one or both must match the current day" So the next run would be on the next February 29 OR the next February monday. You can check this out on https://crontab.guru/#0_0_29_2_1 – Hiero De Paula Dec 17 '21 at 17:09
  • @HieroDePaula Thanks for the info. When I used this hack, it was in a trigger to schedule a WebJob on Azure (I think). The cron job in fact did "work" as reported in my answer, so I expect whatever library Azure is using to derive the dates is not aware of the caveat you've pointed out. – Tim Iles Jan 07 '22 at 16:28
18

Pick a date in the past.

That'll never happen again. Unless it's groundhog day. Just don't pick groundhog day.

Martin Cleaver
  • 909
  • 8
  • 21
0

I created a duplicate (click here to see) for your question, and agree with your initial proposal. After testing, it appears Quartz will never execute a cron expression with a year above 2300.

Community
  • 1
  • 1
Chop
  • 4,267
  • 5
  • 26
  • 58
-3

Opening your crontab file and deleting the entry would be the adequate way. Or you might as well simlink the executable that's called by the cronjob to an empty shell script.

Tell us more about your setup, then we'll see ...

aefxx
  • 24,835
  • 6
  • 45
  • 55
  • I am directly using the expression in one of the framework configuration file ... Apparently i cant put # symbol :-( . If i do my deployment will fail . – Madhu CM Nov 30 '11 at 10:40
  • Doesn't make much sense to me, can you elaborate? – aefxx Nov 30 '11 at 10:49
  • The setup is , i am using a cron expression in xml file and this xml file will be used in one of my framework. I am not executing the cron job using shell script directly. – Madhu CM Nov 30 '11 at 10:58
  • But why can't you put `#` into xml? – Michael Krelin - hacker Nov 30 '11 at 11:02
  • So your cronjob does write to an xml file which in turn is read by one of your frameworks, is that what you're saying? – aefxx Nov 30 '11 at 11:06
  • yes, my cron job expression is defined in xml file. And this xml is read and compiled in framework. If i comment , i will get exception while compiling my xml. My xml should always define one cron job expression and that should not be commented. – Madhu CM Nov 30 '11 at 11:14
  • 1
    . This is the way i am using the cron expression. – Madhu CM Nov 30 '11 at 11:36
  • 1
    Seems like your approach is the only way to effectively prevent the job from executing. Can't think of another way if it needs to stay in place. – aefxx Nov 30 '11 at 11:50
-11

Comment it out — put # sign in front of it. Very useful, especially if you're afraid you'll forget about your changes by the year 3099.

Another possibility is to make it execute dummy command, like true rm -rf /.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173