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.