[SOLVED] Scheduling a CRON job to run on the first Sunday of the month
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Scheduling a CRON job to run on the first Sunday of the month
Does anybody know how to tell cron to run a job on the first Sunday of the month? I can tell it to run every 4th sunday, but that doesn't quite work for backups. Any ideas?
In my opinion it is not possible using the time and date fields of the crontab entry. But you can always check for date at the very beginning of your script and execute the rest only if day of the month is less than 7. For example:
Code:
05 4 * * 0 /some/dir/my_scrip.sh
execute my_script.sh every Sunday, and at the beginning of the script:
Code:
#!/bin/bash
if [ `date +%d` -gt 7 ] ; then
exit
else
<my commands>
fi
Ok, it didn't work right. This last Sunday (Feb 11th) the backup that was only supposed to run on the first Sunday of the month, ran. Here is the script (/etc/.full-system-backup.sh) that I used:
Code:
#!/bin/bash
if [ 'date +%d' -gt 7 ] ; then
exit
else
tob -rc /etc/tob/disk.rc -full system
fi
In /etc/crontab is this entry:
Code:
10 0 * * 0 root /etc/.full-system-backup.sh
The way I read it, it is supposed to work like this:
Every Sunday at 12:10AM crontab runs the script /etc/.full-system-backup.sh
That script runs a check to see if the day of the month is greater than 7
If the DOM is greater than 7, the script exits doing nothing
If the DOM is not greater than 7, the script runs the command "tob -rc /etc/tob/disk.rc -full system" and exits when that is complete
The command "tob -rc /etc/tob/disk.rc -full system" works for running a backup of my system configuration when run from the command prompt. The backup completed without errors, just on the wrong day.
I have limited experience with scripts so I'm not sure what went wrong with the above script. To my mind, the same thing would happen if this were used:
Code:
#!/bin/bash
if [ 'date +%d' -lt 8 ] ; then
tob -rc /etc/tob/disk.rc -full system
else
exit
fi
Of course, I don't want the same thing to happen, I want it to actually work right. Any Ideas?
Uh oh... it can pass unnoticed, but looking at the script I posted above and the one you have just posted, the only and unique difference does the trick:
Code:
if [ 'date +%d' -gt 7 ] ; then # this is wrong
if [ `date +%d` -gt 7 ] ; then # this is correct
the former has single quotes enclosing the date command, the latter has reversed single quotes. The reversed single quote is a bash syntax which gives the result of the command enclosed. Normal single quotes, instead, give the string as is. You can verify the different behaviour issuing the following commands
Code:
echo 'date +%d'
echo `date +%d`
Hence in your script the condition is always false! Furthermore, I wonder why it did not give an error, since the -lt operator is for arithmetic comparison only and should not accept strings as arguments. By the way, the reversed quotes are the answer to the question!
No baluis "0 9 1-7 * 0 /script.sh" will run script.sh every sunday at 9 a.m. and on each the first seven days of the month as well. The day of month and day of week fields are OR'd not AND'd.
I prefer this form because you can see what you are running.
Uh oh... it can pass unnoticed, but looking at the script I posted above and the one you have just posted, the only and unique difference does the trick:
Code:
if [ 'date +%d' -gt 7 ] ; then # this is wrong
if [ `date +%d` -gt 7 ] ; then # this is correct
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.