10

I have below crontab scheduled for Saturday that falls between days 19-23, I' m not sure why it ran on 20th (Friday). Any guesses?

00 21 19-23 * 6 <command>
simer
  • 173
  • 1
  • 8
  • 1
    You might find some inspiration in `/etc/cron.d/mdadm` used on Ubuntu and Debian. This is how it runs the first Sunday of each month: `57 0 * * 0 root if [ -x /usr/share/mdadm/checkarray ] && [ $(date +\%d) -le 7 ]; then /usr/share/mdadm/checkarray --cron --all --idle --quiet; fi` – kasperd Nov 23 '15 at 20:00
  • thanks kasperd, `0 18 * * 6 [date +\%d -le 07] && ` is working well for me, where it is required to run on first Saturday of every month. – simer Nov 30 '15 at 14:01

1 Answers1

16

That Cron expression translates to:

At 21:00 on the 19, 20, 21, 22 and 23rd of every month and every Saturday.

So it explicitly told cron to run on Friday the 20th. This is because of:

When the schedule specifies both date and weekday, they're combined with a logical OR,
i.e. the job will run if current_minute == scheduled_minute 
&& current_hour == scheduled_hour && current_month == scheduled_month && 
(current_day == scheduled_date OR current_weekday == scheduled_weekday).

This information is from this handy Cron tool: http://crontab.guru/

To make your job to run on given days when it is Saturday you could use:

00 21 19-23 * * test $(date +%u) -eq 6 && command

This solution is from crontab day of week vs. day of month?

Madoc Comadrin
  • 1,147
  • 4
  • 13
  • 24