This Blog primarily focus on providing solutions for administering Linux Operating system in Enterprise environment. I have put my best effort to ensure the solutions provided in this are accurate, easy to follow, effective, and helpful for Linux Users & System Administrators.
You can use this post as a reference to schedule the Cronjobs in more Granular fashion.
We had a requirement to execute couple of scripts on specific Saturdays of a month throughout the year i.e., First script (say /opt/firstscript.sh) should be executed on all Saturdays EXCEPT first Saturday of the month, and the Second script (say /opt/secondscript.sh) should be executed ONLY on first Saturday of the month. This we required to do for scheduling Weekly Tape backups.
Executing a script on all Saturdays is fairly simple, we need to just specify number 6 on 5th column on crontab entry, but here the scenario is different.
The solution for this is shown below. This uses ‘date’ command to check if the current date is a first Saturday of a month.
0 2 * * 6 [ $(date +%d) -gt 7 ] && /opt/firstscript.sh # Executes at 2am on all Saturdays Except the first Saturday of the month (Saturdays other than 1st can fall only after 7th ).