Linux - Software This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-01-2007, 12:18 PM
|
#1
|
Member
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 153
Rep:
|
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?
|
|
|
02-01-2007, 12:31 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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
|
|
|
02-01-2007, 12:51 PM
|
#3
|
Member
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 153
Original Poster
Rep:
|
PERFECT!!!!
That does exactly what I need. Thank you very much.
|
|
|
02-12-2007, 03:02 PM
|
#4
|
Member
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 153
Original Poster
Rep:
|
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?
|
|
|
02-12-2007, 03:18 PM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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! 
Last edited by colucix; 02-12-2007 at 03:23 PM.
|
|
|
02-12-2007, 03:56 PM
|
#6
|
Member
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 153
Original Poster
Rep:
|
D'OH
Thanks.
|
|
|
05-27-2011, 07:00 AM
|
#7
|
LQ Newbie
Registered: May 2011
Posts: 1
Rep: 
|
#Should run every first Sunday of each month at 9AM (first Sunday is only when day number is between 1 and 7)
0 9 1-7 * 0 /script.sh
|
|
|
11-25-2011, 07:36 AM
|
#8
|
LQ Newbie
Registered: Nov 2011
Posts: 1
Rep: 
|
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.
0 9 1-7 * * [ "$(date '+\%a')" == "Sun" ] && <path>/script.sh
|
|
1 members found this post helpful.
|
04-11-2013, 11:10 AM
|
#9
|
Member
Registered: Sep 2003
Location: GMT -08:00
Distribution: Ubuntu, RHEL/CentOS, Fedora
Posts: 234
Rep:
|
Quote:
Originally Posted by kakapo
I prefer this form because you can see what you are running.
0 9 1-7 * * [ "$(date '+\%a')" == "Sun" ] && <path>/script.sh
|
Wow! I've been using the "m h * * Sun [ date +%d -le 7 ] && ..." form for years but this is *far* clearer and more flexible. Thanks
Last edited by archangel_617b; 04-11-2013 at 11:24 AM.
Reason: brevity
|
|
|
08-05-2013, 11:42 AM
|
#10
|
LQ Newbie
Registered: Aug 2013
Posts: 1
Rep: 
|
Quote:
I prefer this form because you can see what you are running.
0 9 1-7 * * [ "$(date '+\%a')" == "Sun" ] && <path>/script.sh
|
This worked for me, with one exception. Everytime cron ran this, it would fail with
Code:
[: 1: Thu: unexpected operator
("Thu" being which ever day of the week it ran).
I removed one = sign, changing the code to
Code:
0 9 1-7 * * [ "$(date '+\%a')" = "Sun" ] && <path>/script.sh
Now it works. This was on a machine running Debian 7.
FWIW.
|
|
|
11-08-2016, 12:25 PM
|
#11
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Rep: 
|
Quote:
Originally Posted by colucix
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
)
|
this one is also OK:
if [ $(date +%d ) -gt 7 ]; then ...
|
|
|
All times are GMT -5. The time now is 07:23 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|