Cron for an alternate sunday


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron for an alternate sunday
# 1  
Old 05-21-2012
Cron for an alternate sunday

Hi again,

I need to delete stats on every 2nd Sunday and I've try the following codes but unfortunately it didn't work. I'll really appreciate as always for your help. I'm on FreeBSD 8.2-STABLE.

altsun.sh script:

Code:
#!/bin/sh
day = $(`date '+%e'`)
if [ $day -ge 8 -a $day -le 14 ] then
   rm /home/user/tst/statsmod.dat
else
exit
fi

Code:
59 23 * * 0 /home/user/altsun.sh >/dev/null 2>&1


Sincerely Regards,
user
# 2  
Old 05-21-2012
If your cut/paste is accurate, your problem is likely because you have spaces round the equal sign in the assignment of day:

Code:
day = $(`date '+%e'`)

However, I question your logic. Deleting your file only when the execution day is the 8th through the 14th only cleans up once per month. If you want to delete every other week, wouldn't it be better to delete when the week number is either odd or even?

Code:
if (( $(date +%U ) % 2 ))
then
    rm some-log-file
else
   exit 0
fi

The above should delete on odd weeks and should do the right thing when there are 5 Sundays in a month.
This User Gave Thanks to agama For This Post:
# 3  
Old 05-21-2012
Thanks so much agama! I like your approach and will try it. Thanks again
# 4  
Old 06-04-2012
Hello agama,

I have tried your codes past two sundays and I was expecting on 06/03/2012 (it was odd sunday) the script should have deleted the stats but it seems it didn't work. Am i missing something?

cron:
Code:
59 23 * * 0 /home/user/altsun.sh >/dev/null 2>&1

altsun.sh:
Code:
#!/bin/sh

if (( $(date +%U ) % 2 ))
then
   rm /home/user/tst/statsmod.dat
else
   exit 0
fi

Thanks again
best regards
user
# 5  
Old 06-04-2012
I don't think so.

Given that it's an "odd week" all week, what happens if you run this script?

Code:
if (( $(date +%U ) % 2 ))
then
   echo "it is an odd week and things should be removed"
else
   echo "it is an even week and things would not be removed"
fi



You can run this manually to see what is happening.

You might also want to redirect your output and stderr from the script to a file rather than /dev/null. If there's an error in the script you'll be able to catch it. The way you have it coded you'll be scratching your head if there is a problem. I always like to have a log of what is kicked off by cron and usually put echo statements at key points (deleting files, skipping files in this case) so as to document that the script was run, and to know what it thought it did. Also to capture any error messages along the way.

---------- Post updated at 22:03 ---------- Previous update was at 22:00 ----------

I just noticed that you're running it with #!/bin/sh. If on your system that points to a "real" Borne shell (and not a more recent shell), then the syntax $(...) and ((..)) might not be valid.

Try replacing #!/bin/sh with the path for bash or kshell (in addition to adding some logging).
# 6  
Old 06-04-2012
agama, thanks for the reply

well i think bash or kshell won't work because i'm using freebsd (/bin/tcsh).

I'll definitely add logging as you have suggested. So it will work on next sunday.


thanks
user
# 7  
Old 06-05-2012
Yes, if you don't install bash/kshell it's probably not there by default.

Given that, the /bin/sh on FreeBSD (at least on the 8.2 system I tested on) is a more Borne like (probably POSIX) shell, and it's likely tripping over the if expression. When I tested your original code it evaluated the expression, but then tried to execute the result (1) rather than treating that as true. I just tried the code below and it seems to do just fine:

Code:
#!/bin/sh

w=$(( $(date "+%U") % 2 ))
if [ $w -ne 0 ]
then
        echo odd week
else
        echo even week
fi



You might want to try changing your script to something like this. You could always code a dummy script (with echo statements rather than the rm statement), and set it off from cron before next Sunday rather than waiting all week.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

cron job on 3rd sunday of every month

Hi, Actually scheduled my test scripts on test severs as shown below. They are supposed to run on 3rd sunday of every month. Unfortunately it ran on 2nd sunday of the month (suspecting that it will run every sunday). I am sorry if I miss something. Could you please let me know if I did any... (1 Reply)
Discussion started by: System Admin 77
1 Replies

2. UNIX for Dummies Questions & Answers

Yesterday's Date if it is Sunday

Hi All, Can you please let me know how to get the yesterday's date of the given date if the given date is Sunday? I can't use GNU. I have the code to get the yesterday's date based on the system date. Thanks (5 Replies)
Discussion started by: shash
5 Replies

3. Shell Programming and Scripting

How to get first sunday of the month?

Hi, Please can someone help me in getting first sunday date of a month. i_year=`date +%Y` ny_first_sun_nov=`cal 10 $i_year | sed '/^$/d' |head -3 |tail -1| rev | cut -c1` This works good if the first sunday has a value but not if it is blank and first sunday falls on second week. ... (17 Replies)
Discussion started by: infyanurag
17 Replies

4. Shell Programming and Scripting

Second sunday of March

Hi, I want to get the second Sunday of march in any year, I have tried below command but it is not giving me the correct output i_year=`date +%Y` cal -m 03 $i_year | sed '/^$/d' |head -4 |tail -1|rev | cut -c1` This is returning me 0 , where as I want 10. Can you please help Thanks (5 Replies)
Discussion started by: infyanurag
5 Replies

5. UNIX for Advanced & Expert Users

How to get the count of sunday between two dates?

Hi Am using unix Ksh I have the two dates DATE1=01/01/2013 DATE2=11/02/2013 In this two dates i need the output as count of sunday sunday=6 Can anyone help me pls!!! (1 Reply)
Discussion started by: Venkatesh1
1 Replies

6. UNIX for Advanced & Expert Users

How to get the sunday days between two dates?

Hi Am using Unix Ksh I have a two date input as DATE1=02/12/2012 DATE2=30/12/2012 I Need the output as only sunday date 02/12/2012 09/12/2012 16/12/2012 23/12/2012 30/12/2012 can anyone pls help me.. thanks in advance... (2 Replies)
Discussion started by: Venkatesh1
2 Replies

7. Solaris

How to check for Saturday or Sunday

Hi , I have a date parameter passed in YYYYMMDD format , how can I check whether it is Sat or Sun on Solaris box , as we can do the same easily on linux box by using date -d YYYYMMDD '+a' . Any pointres will be really helpful . (5 Replies)
Discussion started by: harpreetanand
5 Replies

8. UNIX for Dummies Questions & Answers

Cron job -- to execute at every first Sunday of every month

Dear all How can I schedule the cronjob to be run sometime at every first Sunday at every month? I have the edit the cronjob every month now, thanks (2 Replies)
Discussion started by: shanemcmahon
2 Replies

9. Shell Programming and Scripting

Date - Last Sunday thru Saturday

Hi All, I have to kick off a script on every Monday to get some data from database for last week (Sunday thru Saturday). If its Monday (12/12/2005) Begin date will be Sunday - 12/4/2005 End date will be Saturday - 12/10/2005 The script might not kick off on some Mondays. So my... (2 Replies)
Discussion started by: skymirror
2 Replies
Login or Register to Ask a Question