Recurring Job every X weeks?

I know Cron doesn’t specifically support this but I’m wondering if there is a workable solution with Hangfire.

I need to run some jobs every 2 weeks on a Thursday is there a way to schedule like that?

Please note that I am running Hangfire 1.7.0-beta so I have the Cronos library (it is really a great improvement).


In Cron syntax, day-of-week and day-of-month rules are not restricting but complementing each other, so if you specify something like "0 0 */14 * 2", it will run both on Tuesdays and every two weeks.

The best shot is probably to schedule a job every Tuesday, and then check if the execution is actually needed (by storing execution date somewhere, and then calculating date difference between the current date and previous execution date).

1 Like

Thanks, I was hoping to avoid all of that but if that is the only way I might have to go in that direction.

A long time ago I wrote a cron extension (using Ncrontab Advanced) that would allow use of “X” character on day of week.

So something like:
“00 00 * * FRIX2”

Would run every second Friday by running cron.getnextoccurance 2 times and use the second one to get 2 weeks from the current time.

But you need a “start” context to figure out the X2 so it could only be done within the job itself instead of relying on cron to actually run it (in Hangfire terms it couldn’t be a recurring job, just a scheduled job that submits another scheduled job).

Anyway I’ll see if anyone else has a suggestion but you are probably correct that I’ll have to build something.

Since you’re using Cronos, you can also try playing around with # specifier, e.g. "0 0 * * 2#1,2#3". But some months may have 5 Tuesdays, so sometimes it would be 21 (or 7, if you also add 2#5 specifier) days between executions instead of 14. Keeping it exactly two weeks would require some coding.

UPD: oops, it actually appears to be a bug in Cronos, which doesn’t allow to mix # with ,. So never mind.

Thanks I tried and like you said Cronos doesn’t like the mixing the # with a comma.

I think maybe I can convince some users to do 2#1 and 2#3 so it will be twice a month running. It looks like to do that in Cronos I’ll have to set 2 jobs:
Job1 “0 0 * * 2#1”
Job2 “0 0 * * 2#3”

I’ll keep thinking about how to code something else for every 2 weeks. I might just have to chain jobs with getnextoccurance like I have in the past.

Looks like there’s a feature request on GitHub for something similar. I’ve chimed in there to support and explain how I did something similar before.