Connecting Tech Pros Worldwide Help | Site Map

how do i get my cron to run only on weekends

Newbie
 
Join Date: Nov 2008
Posts: 11
#1: Jan 19 '09
Hello people,
I have a daily script which i execute it manually on daily basis..
1.On week days i need to run this script on 8 in the morning
2.ON week ends i need to run this at 8 in the night (only saturday and sundays).
3.Also In above two scenarios the 1st of every month should not be included and some special scipt runs on 1st of every month..

I could manage to find the solution for the 8 in the morning issue..
But failed for the week ends..

Below is the entries in the cron, the first one works fine but the second one(WEEKENDS) executes on week days as well as week ends....

##WEEKDAYS
00 08 2-31 * 1-5 /usr/.............

##WEEKENDS ()
00 20 2-31 * 0,6 /usr/........

PLease help, i just need to run only on saturday and sundays at 8 in the night except the 1st of every month........
Newbie
 
Join Date: Nov 2008
Posts: 11
#2: Jan 19 '09

re: how do i get my cron to run only on weekends


Look there is a catch in the WEEKENDS cron entry..
I dont want to run this entry on 1st of month, but rather it shud run only on saturday and sunday accept 1st of every month...
Newbie
 
Join Date: Nov 2008
Posts: 11
#3: Jan 20 '09

re: how do i get my cron to run only on weekends


Can anybody help me.............................
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#4: Jan 21 '09

re: how do i get my cron to run only on weekends


Quote:

Originally Posted by anirudhbapu View Post

Hello people,
I have a daily script which i execute it manually on daily basis..
1.On week days i need to run this script on 8 in the morning
2.ON week ends i need to run this at 8 in the night (only saturday and sundays).
3.Also In above two scenarios the 1st of every month should not be included and some special scipt runs on 1st of every month..

I could manage to find the solution for the 8 in the morning issue..
But failed for the week ends..

Below is the entries in the cron, the first one works fine but the second one(WEEKENDS) executes on week days as well as week ends....

##WEEKDAYS
00 08 2-31 * 1-5 /usr/.............

##WEEKENDS ()
00 20 2-31 * 0,6 /usr/........

PLease help, i just need to run only on saturday and sundays at 8 in the night except the 1st of every month........

Sorry for the delay in trying to help you.

Cron itself is a funny beast. You will find that shortcuts or methods that work on one machine's cron may or may not work on anothers. This is typically due to different versions of cron.

To see the manual page that explains the sections of the crontab, use this:

Expand|Select|Wrap|Line Numbers
  1. man -s 5 crontab
  2.  
As for your issue on the weekends, have you tried specifying the days using the day names? Here is an example:

Expand|Select|Wrap|Line Numbers
  1. ##WEEKENDS ()
  2. 00 20 2-31 * Sat,Sun /usr/........
  3.  
If that does not work, then try something like "6,7", where 6 is Saturday and 7 is Sunday, instead of 0,6 as you have it.

Again, you just have to play with crontab to get everything right and working properly.

Regards,

Jeff
Newbie
 
Join Date: Nov 2008
Posts: 11
#5: Jan 21 '09

re: how do i get my cron to run only on weekends


Thanks Jeff for the reply....
i have checked and read the manual for the crontab very carefully..but it doesnt mention properly about the weekend stuff which i require....
They have just explain the overview for the same...
Also i tried with 0,6--what it does with is ....it execute the cron at 0 (Sunday) and 6 (Saturday) and also it executes on 2-31 which i dont want....

According to your suggestion if i put 6,7..the entry will look like below...
00 20 2-31 * 6,7 /usr/.../.../.../ > /dev/null 2>&1....

the above cron entry again says that the command will execute on 6 and 7 (i.e saturday and sunday) and also it will run on 2-31..

I hope u got me, what iam trying to say......

What i exactly want is---i have only one script, i need to execute that script on week days at 8 in the morning and on week end it will execute at 8 in the night..Also the cron entry should take care that it should not run on 1st of every month.....
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#6: Jan 21 '09

re: how do i get my cron to run only on weekends


I think you are contradicting yourself. You said in your original post you want this to run all days but the 1st and in this one, you said: "and also it executes on 2-31 which i dont want". Not to question you, but do you or don't you want it running on every day but the 1st? If you only want it run on the 1st, just put a 1 in place of the 2-31.

Regards,

Jeff
Newbie
 
Join Date: Nov 2008
Posts: 11
#7: Jan 22 '09

re: how do i get my cron to run only on weekends


Sorry..i guess u took me wrong..let me explain once again...

I have placed 2 lines in cron..
one for week days to be executed at 8 in the morning
And other at week ends to be executed at 8 in the night..

BUT BOTH THE SCRIPT ARE SAME BUT WITH DIFFERENT TIMINGS...

1.
##WEEKDAYS
00 08 2-31 * 1-5 /usr/.............

2.
##WEEKENDS ()
00 20 2-31 * 0,6 /usr/........

Now as per the first entry it will run on all weekdays (From mon to friday (1-5) and 2-31 ie; on all days except sat/sun and 1st of every month)..

Now if u see the second entry its mentioned 0,6 that is sat and sunday also it is mention 2-31...this mean over here it is running on sat and sun as well as 2-31 ie;everyday..THIS IS THE THING I DONT WANT.I WANT THE SECOND ENTRY TO BE RUN ONLY ON SAT AND SUN BUT NOT ON 1st.
So the same script is executing at sat and sun as well as all the weekday which is already executing in 1 entry at 8 in the morning...

How should i make the 2nd entry possible to be executed by cron only on weekends and no other day and not on 1st of every month ofcourse.....

I HOPE U UNDERSTOOD MY PROBLEM.....
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,256
#8: Jan 23 '09

re: how do i get my cron to run only on weekends


For the second cron...start the script on all saturday and sunday and inside the script use the date command to determine the date and then if its 1st of the month then exit.


Raghu
Newbie
 
Join Date: Nov 2008
Posts: 11
#9: Jan 23 '09

re: how do i get my cron to run only on weekends


Can you pls tell me the exact entry for sat and sun only..and also the date script to be put into my script..

will that be like below.....
if [ `date "+%d"` -eq 1 ]
then
echo " Script wont be executed...... "
exit 1
else
Reply