Cron Expression for every second Monday of the month (for Hangfire)
Asked Answered
T

5

6

I am trying to create recurring job in hangfire that runs, once a month at the second Monday, something like this: 1. Monday, May 14, 2018 8:00 AM 2. Monday, June 11, 2018 8:0 AM 3. Monday, July 9, 2018 8:00 AM 4. Monday, August 13, 2018 8:00 AM 5. Monday, September 10, 2018 8:00 AM

I have found this answer in stackoverflow, but since this is not a standard cron for scheduling hangifre jobs I can not use it.

My question is can I make an expression like this using the format * * * * * (min hour day/month month day/week)

Thither answered 13/4, 2018 at 8:54 Comment(0)
T
3

The following command seems to work for me.

0 8 ? * MON#2

Assuming that you want this job to execute at 8 AM the second Monday of each month, the # character allows you to specify the "nth" day of any given month. We use the ? character in the day/month row since we are fine with any numeric day as long as it is the second Monday.

Read more about special characters here: http://www.quartz-scheduler.org/documentation/quartz-2.2.2/tutorials/crontrigger.html#special-characters

Transarctic answered 25/8, 2022 at 16:7 Comment(0)
F
0

Below are cron for three different time for every 2nd Monday, Look into the pattern and make change in time as per your need day

For each second Monday of the month at 00:00 hrs, try below: 0 0 0 ? 1/1 MON#2 *

For each second Monday of the month at 10:30 hrs, try below: 0 30 10 ? 1/1 MON#2 *

For each second Monday of the month at 13:30 hrs, try below: 0 30 13 ? 1/1 MON#2 *

Finis answered 8/10, 2021 at 10:58 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gur
P
0

Use of a day-of-week like Mon#2 as suggested in other answers is non-standard and is not implemented by ISC Cron (the Vixie Cron derivative used by most systems, including most Linux distributions and BSDs). It's certainly not in the crontab(5) page for Debian's latest Testing package of cron 3.0pl1-186+b1.

More pertinent to this question, which asks about the Hangfire for C#, the Performing Recurrent Tasks documentation is ambiguous, noting simply that "you can also use CRON expressions to specify a more complex schedule", relying on that Wikipedia link to provide further detail. Digging further, the HangfireIO Cronos project, which I assume is the underlying cron implementation, does in fact note that it supports the non-standard L, W, and # characters.

That means Hangfire users can indeed use 0 8 * * Mon#2 to run at 8a on the second Monday of every month. (Other answers here use a question mark, but HangfireIO's docs say that's synonymous with * in their implementation, so I see no reason to add another non-standard character.)


Here's a solution without non-standard characters that should work on Posix-style systems like BSD and Linux:

#m  h   dom mo  dow command
0   8   *   *   Mon d=$(date +\%e) && [ $d -ge 8 ] && [ $d -le 14 ] && echo "2nd Monday"

Since cron can't handle this on its own, we invoke date to get the day of the month and then ensure it's the 8th through the 14th. We could alternatively have implemented this as 0 8 8-14 * * [ $(date +\%w) = 1 ] && echo "2nd Monday", but that cron job runs (and does nothing) six times a month (72 times a year) while this cron job runs (and does nothing) 3-4 times a month (40 times a year), thus polluting your syslog less.

Since Hangfire is C# and probably not run on a Posix-style system, you shouldn't rely on Bash/Posix shell logic or the Posix date command. This answer is here for completeness since # is not standard.

Do note that 0 8 8-14 * Mon will not work because cron sees days of month and days of week as unions rather than intersections; this command will run on any day that is the 8th to the 14 of a month OR any Monday.

Petuntse answered 11/3 at 2:57 Comment(0)
P
-1

Here you go.

0 0 12 ? 1/1 MON#2 *
Predella answered 26/4, 2021 at 8:19 Comment(3)
Consider adding an explanation of why this works.Acme
This can't work as cron only has 5 fields for time.Mirk
which cron implementation is this? does not seem standard to meLoving
M
-3
minute   hour   day   month   dayofweek   command   
0        0      8-14  *       2           /path/here

This will run a job every second tuesday of the month at midnight.

8-14 limits the occurance of tuesday to the second week in the month.

1-7 first week
8-14 second week
15-21 third week
22-28 forth week
29-31 fifth week
Mirk answered 14/4, 2018 at 9:5 Comment(2)
I don't think that this will work. In my opinion this expression is equal to “At 00:00 every day from 8 through 14 and on TuesdayThither
Hmm.. that's odd, but yeah - it even says so in the English Wikipedia article to Cron - didn't know that this special rule even exists.Mirk

© 2022 - 2024 — McMap. All rights reserved.