Executing a scheduled task on the initial Monday of each month?

Feedback

Question:

At 8:30 on the first Monday of each month, I would like to schedule a job to run via cron. According to the Wikipedia page on cron, this is possible.

Typically, the task is performed when the date and time specifications coincide with
current time
. However, there is an exception if both “day of month” and “day of week” are constrained (not “*”). In such a case, either the “day of
month” field
(3) or the “day of week” field (5) needs to correspond with
current day
.

(my emphasis)

So, am I only allowed to do the initial
Monday of the Month
on the first day of the month, or is there any other way to accomplish it? I can’t seem to find a workaround.



Solution 1:

It is possible to include the requirement within the crontab command itself, using a universal approach.

[ "$(date '+%u')" = "1" ] && echo "It's Monday"

If you are in the EN/US locale, you have the option to perform string comparisons as well (original response).

[ "$(date '+%a')" = "Mon" ] && echo "It's Monday"

If the aforementioned situation is valid during the initial seven days of a month, the first Monday can be determined. It should be noted that in the crontab, the percent-syntax needs to be escaped.

0   12  1-7 *   *   [ "$(date '+%u')" = "1" ] && echo "It's Monday"

In case your region is set to EN/US, it is also possible to make a comparison between strings (original response).

0   12  1-7 *   *   [ "$(date '+%a')" = "Mon" ] && echo "It's Monday"

Substitute the

echo

function with the desired command that you intend to execute. I also stumbled upon a comparable technique.


Solution 2:


Due to my computer’s Spanish locale, the method is ineffective for me as the abbreviation “mon” transforms into “lun”.

Since other languages would also not work, I made a small modification to the approved solution that eliminates the issue of language barriers.

 0 9 1-7 * *   [ "$(date '+%u')" = "1" ] && echo "¬°Es lunes!"


Solution 3:


It’s less challenging for me when I don’t have to deal with numerical dates.

Run First Monday of month:

0 2 * * 1 [ `date '+%m'` == `date '+%m' -d "1 week ago"` ] || /path/to/command

In case the month 1 week earlier differs from
current month
, then it means that the current day is the 1st day 1 (which is Monday) of the month.

Similarly, for the Third Friday

0 2 * * 6 [ `date '+%m'` == `date '+%m' -d "3 weeks ago"` ] || /path/to/command

If the month that occurred three weeks ago is not the same as the current month, then we are currently on the 3rd day 6 of the month (which is a Friday).



Solution 4:

I have arranged for a task to occur every month on the fourth Monday, specifically at 4:00 PM.

0 16 22-28 * Mon [ "$(date '+%a')" == "Mon" ] && touch /home/me/FourthMonOfMonth.txt

Frequently Asked Questions