Score:0

Crontab - day of month seems to override day of week?

sa flag

I have five servers running an eventstore cluster. I want to run a crontab command on each one on separate days.

  1. Run on Monday
  2. Run on Tuesday etc

But I also want to run a different command on the fourth week of every month.

I set them up like this, thinking it should work - and checking crontab.guru confirmed what I thought should happen;

Machines 1-5:

  1. 30 6 1-21 * Mon /first-command-to-run
  2. 30 6 1-21 * Tue /first-command-to-run
  3. 30 6 1-21 * Wed /first-command-to-run
  4. 30 6 1-21 * Thu /first-command-to-run
  5. 30 6 1-21 * Fri /first-command-to-run

Then a similar command on each for the Week 4 command:

  1. 30 6 22-29 * Mon /week4-command-to-run (repeat DOW as above)

So for today - Friday the 19th, I wanted just machine #5 to run /first-command-to-run at 0630 and for the other four to do nothing.

I was fairly confident this would work as expected, but it seems all five machines ran /first-command-to-run this morning, instead of just Friday's machine.

None ran the /week4-command-to-run though, so the day of month falling outside of the 22-29 range is correctly preventing that.

I'm now wondering if by specifying the day-of-month range, this is causing cron to disregard the Day-of-Week. Could that be the case? If not, what am I doing wrong?

Os is Rocky 8.4

(Note: I'm aware and okay with the fact that the exact day-of-months might fall outside the strict weeks.)

Simon Avery avatar
sa flag
Thanks to @archemar for the pointer. For anyone following, this will do as I expected: 30 6 1-21 * * [ "$(date --date='Today' '+\%a')" = "Tue" ] && /first-command-to-run
Score:0
mx flag

according to man 5 crontab on ubuntu.

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., don't start with *), the command will be run when either field matches the current time.

Please, do check your local crontab man page.

Simon Avery avatar
sa flag
Gah, Don't know how I missed that. Thanks for the reply, that's definitely the issue. Looks like I'll need to do some smarter logic on the script itself.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.