How to schedule a cron to execute on first monday of every month ?

Suppose you want to run a job once per month, and that too on a particular day.

For example:- You want to setup a cron job to be executed on first monday of every month.

The solution is pretty easy, however a bit tricky hence i preferred to mention it here.

Cron Entry for Job execution on first Monday

0 3 * * mon [ $(date +%d) -le 07 ] && /root/test.sh

In the above command we have following options:

1. 0(MIN 0-59) –> 0th Minute
2. 3(HOUR 0-23) –> At 3 AM.
3. *(DAY OF Month 1-31) –> Any date of the month.
4. *(Month 1-12) –> Every Month.
5. mon (DAY of week) –> On Monday, execute the command.
6. [ $(date +%d) -le 07 ] && /root/test.sh –> The command to execute on every monday.

Now in the above command, if “date +%d” i.e. day of month is less than or equal to(-le) 7 than it will execute the script mentioned.

And the first day of the week comes between 1 to 7 of every month.

Hence we can use above trick to setup this type of cron job.

Similarly second monday comes within 8 to 14 on all months.

Hence we can modify and schedule the job as per our requirement.

In case of saturday, we can replace “mon” with “sat” and so we can schedule the job any day.

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Geeklab.info
Geeklab.info
2 months ago

Yes, you could do that, with “date” to determine if it’s the first monday. But why not stick to the functionality cron already offers? Just make it:

0 3 1-7 * mon /root/test.sh
Geeklab.info
Geeklab.info
Reply to  Geeklab.info
1 month ago

So… i found out why. My suggestion does:
if minute==0 AND hour==3 AND (day between 1-7 OR monday)
I was not aware of this weird inconsistency