fjordtools / Errors / Cron: day of week vs day of month

Cron day of week and day of month: OR, not AND

When the day-of-month and day-of-week fields are both restricted, standard cron runs the job on days that match either one, not both. So 0 9 1 * 1 runs on the 1st of every month and also on every Monday. A job for the first Monday of the month needs a check inside the command.

Fjordtools team · Seen in Vixie-style cron on Linux and macOS · Published · 1 min read

Common causes

  1. Both fields set

    The explainer says it: on the 1st of the month, or on Monday. Every Monday and the 1st both run.

    Input

    0 9 1 * 1

    Cron Explainer says

    At 09:00, on the 1st of the month, or on Monday
  2. Only one of them set

    With a star in the other field, there is nothing to combine and the line means exactly what it says.

    Input

    0 9 * * 1

    Cron Explainer says

    At 09:00, on Monday
  3. A range of days plus a weekday

    1-7 is the first week of the month. Hoping for the first Monday, you get the first seven days and every Monday.

    Input

    0 9 1-7 * 1

    Cron Explainer says

    At 09:00, on the 1st, 2nd, 3rd, 4th, 5th, 6th and 7th of the month, or on Monday
  4. A day that is not every month

    Day 31 never runs in a month with 30 days. February 30 never runs at all.

    Input

    0 0 31 2 *

    Cron Explainer says

    At 00:00, on the 31st of the month, in February

How to fix it

  1. Set only one of the two fields and put the other condition in the command, for the first Monday: 0 9 * * 1 [ "$(date +\%d)" -le 7 ] && your-job.
  2. In crontab, write each percent sign as \% or it is read as a line break.
  3. Use a scheduler that combines them, such as a systemd timer with OnCalendar=Mon *-*-1..7 09:00.
  4. Paste the line into the cron explainer: the next run times show the days that really match.

Related errors

More error explainers are coming

See all errors.