Jenkins Scheduling

Hi All:

I have a question regarding scheduling pipelines in Jenkins. As I understand it, Jenkins uses a cron syntax for scheduling (ex: minute hour daymonth month dayweek). My question is around the daymonth variable. If I used 31 as a value, would the pipeline fire on say the 28th of Feb or the 30th of April? My assumption is that it would not, but I haven’t been able to find any documentation indicating it would or would not work.

Thanks in advance.

what is it you are trying to schedule? Like the last day of the month?

or a hypothetical, what happens if I use a date number during a month that doesn’t go that high?

if it’s the latter, a script set to execute on the 31st of a month that doesn’t have 31 days won’t kick off on Sept, Apr, Jun, or Nov and definitely not Feb.

If it’s the former, best knowledge I see is:

  1. cron for every 28 - 31, that executes a script. and the script checks if it’s the end of the month
  2. in some implementations of cron you can use L as a value, but 0 0 L * * Last day of month; but that doesn’t read into Jenkins.
  3. final option is hard code each month, but leap days aren’t accounted for.
H H 31 1 * 
H H 28 2 * 
H H 31 3 * 
H H 30 4 * 
H H 31 5 * 
H H 30 6 *
H H 31 7 *
H H 31 8 *
H H 30 9 *
H H 31 10 *
H H 30 11 *
H H 31 12 *

personally I’d run drop the above into the Build Periodically Schedule… H is a random hash for Minutes and hours in this case. If you need to refine the window you can change to “H(0-4)” for a random value between 0 and 4

For validation, you’ll see text below the box “Would last have run at Tuesday, April 30, 2024 at 4:33:33 AM Eastern Daylight Time; would next run at Friday, May 31, 2024 at 1:10:33 AM Eastern Daylight Time.” or similar

1 Like

Well, I wish it was that simple to be honest :slight_smile:

So basically we have a request system for our support team to put a request in for automation. The result of this request would end up creating a scheduled Jenkins job (which that part we have figured out). Aside from several other questions, the request would be do you want the job to run daily, weekly, monthly or annually (I don’t see annual being requested that often, but that’s what they want). I have all the other frequencies figured out except for the monthly because I would need to ask which days (1 - 31). The issue I have however is if they choose 31, months that do not have a 31 (Ex: Feb, April, June, etc.) in it will most likely not run. I’m not so much concerned with leap years (I think that’s overkill and I’ll fight them on that).

Based on your response though, I suppose if they choose 31, I could format the schedule for the specific months on separate lines (assuming Jenkins supports that). It’d be a little more work on my end, but I think its the only way I’ll get this to work cleanly.

1 Like

You might be able to try “rigging the system” by using the timezone option. If your job needed to run at midnight PST, that’s 0800 UTC on the first Day of Month.

You can use a South Pacific TZ for a similar European offset.
Cloudbees reference to get TZ.

1 Like

I’m writing a POC plugin that allows for an extended syntax based on GitHub - jmrozanec/cron-utils: Cron utils for parsing, validations and human readable descriptions as well as date/time interoperability.

This allows for running on the Last day of the month, third monday of a month, last monday of a month and so on,

1 Like

My plugin is ready now:

You can now use e.g.

0 0 L * * 

to run a job at midnight of the last day in the month so it will run on Jan 31st or Apr30th with just this one line. No need to maintain multiple entries.
Or to run something on the last Sunday of a month at 6 AM use
0 6 * * 0L

Unfortunately the underlying library doesn’t support the H operator to use a random distribution. So you will need to manually distribute the load.

1 Like