LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 02-01-2007, 12:18 PM   #1
New2Linux2
Member
 
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 135

Rep: Reputation: 22
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?
 
Old 02-01-2007, 12:31 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
Old 02-01-2007, 12:51 PM   #3
New2Linux2
Member
 
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 135

Original Poster
Rep: Reputation: 22
PERFECT!!!!
That does exactly what I need. Thank you very much.
 
Old 02-12-2007, 03:02 PM   #4
New2Linux2
Member
 
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 135

Original Poster
Rep: Reputation: 22
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?
 
Old 02-12-2007, 03:18 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
Old 02-12-2007, 03:56 PM   #6
New2Linux2
Member
 
Registered: Jan 2004
Location: Arizona
Distribution: Debian
Posts: 135

Original Poster
Rep: Reputation: 22
D'OH
Thanks.
 
Old 05-27-2011, 07:00 AM   #7
balius
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: Disabled
#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
 
Old 11-25-2011, 07:36 AM   #8
kakapo
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
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.
Old 04-11-2013, 11:10 AM   #9
archangel_617b
Member
 
Registered: Sep 2003
Location: GMT -08:00
Distribution: Ubuntu, RHEL/CentOS, Fedora
Posts: 234

Rep: Reputation: 42
Quote:
Originally Posted by kakapo View Post
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
 
Old 08-05-2013, 11:42 AM   #10
jbmart
LQ Newbie
 
Registered: Aug 2013
Posts: 1

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scheduling monthly job in cron from debian sarge 3.1 sridhar11 Debian 2 10-28-2005 09:03 AM
unable to run cron job fahad26 Linux - General 3 06-30-2005 01:51 AM
how to run this cron job ashley75 Linux - General 5 05-24-2004 11:20 AM
Did my Cron job run? ryedunn Linux - Newbie 2 02-25-2004 08:59 AM
Cron job does not run brentos Linux - General 6 12-12-2003 02:37 PM


All times are GMT -5. The time now is 08:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration