>_ IzzyLab

Hands-on task

This task is step 10 of the course “Cron: scheduled jobs”

First Monday of the month

hard~10 min

In the root crontab, set up the job /opt/monthly-maint.sh to run only on the first Monday of each month at 09:00. Watch out for cron's classic quirk: if you set both the day of month and the day of week, cron combines them with OR (it will fire on both days 1-7 and every Monday) — which is wrong. Work around it. Any entry that fires on exactly those days and at that time will do.

What you'll practise:
  • cron
  • crontab
  • day of week
  • date
  • first Monday

Topic breakdown

Cron has a classic trap: if the schedule sets both the day-of-month field (3rd) and the day-of-week field (5th) at once, they combine with OR, not AND — the job will fire on all the specified dates and on all the specified weekdays. So a condition like "first Monday" can't be expressed with a schedule alone. The workaround is this: narrow the range of dates to the first week with the schedule, and move the exact day-of-week check into the command itself — via a test over the output of date (the %u format gives the weekday number), noting that in a crontab the percent sign requires escaping.

What's checked

  1. the root crontab has a job with monthly-maint.sh
  2. runs exactly on the first Monday of the month at 09:00

What it looks like