Cron job every Friday except 4th


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job every Friday except 4th
# 8  
Old 04-22-2010
Quote:
Originally Posted by danmero
One improvement to speed up Smilie

Code:
if [  $(date +%u) -eq 5 -a $(date +%d) -lt 22 ];then echo OK;else exit;fi

Reference : Man Page for date (All Section 1) - The UNIX and Linux Forums
  • $(date +%u) -eq 5 If week day is 5 (Friday), I put this test first like that we will not try the second test(call date) if the first one is false.
  • -a and
  • $(date +%d) -lt 22 monthly day number less that 22(the script should run in the first 3 weeks/21 days)
  • then echo OK Do something , run your script, etc
  • else exit if one or both condition not true.
Thanks! Although a monthly backup may take place on say the 1st. It's not necessarily the last Friday of the month. I'm not really sure how I can write this down clearly. There is an example in post 3 ^

If I understand your code that will run it the first, second and third Friday, but not the fourth of each month, whereas we require it to run on every fourth week.

I was thinking along the lines of using an initial date, then +28 days (multiple). Since there is no set date as such the monthly backups take place, its every four weeks. So we could start the cycle in the script on whatever date. For example, I know our next monthly backups begin on Friday 30th April 2010. Here is a few dates after this to try and give more of an idea of the cycle -

30th April (Just happens to be the last Friday)
28th May (Just happens to be the last Friday)
25th June (Just happens to be the last Friday)
23rd July (Not the last Friday, it's in fact the 4th out of 5 Fridays in July)
20th August (Not the last Friday either, it's the 3rd out of 4)

The reason it is set a little odd like this is because it's the weekend after our pay (we get paid 4 weekly, not monthly).

Hope this makes sense.

Thanks for the replies!
Rab



Edit - Poor attempt at pseudo like code below

Cron Entry - 00 21 * * 5 /backupscript

backupscript -
Code:
if todays date is +28 (multiple) 30th April 
then run monthly backup
else run normal backup
fi

There is probably a better way to do it, I don't know enough about scripting to get my head around it Smilie



Edit 2 - This is AIX v5.3 if it makes any difference.

Last edited by rab; 04-22-2010 at 04:00 PM.. Reason: Added a bit at the end
# 9  
Old 04-22-2010
Any solution will need to know when the last monthly backup ran or when the next monthly backup is scheduled to run, perhaps by having that info stored in a file by the monthly backup process. With that info in hand, it should be easy enough for the daily to decide if it should run on any given friday.

Regards,
Alister
# 10  
Old 04-22-2010
Quote:
Originally Posted by alister
Any solution will need to know when the last monthly backup ran or when the next monthly backup is scheduled to run, perhaps by having that info stored in a file by the monthly backup process. With that info in hand, it should be easy enough for the daily to decide if it should run on any given friday.

Regards,
Alister
Good point.

Perhaps adding into the monthly backup script a touch command to create a file with todays date +28 days.

Then on the call script in the cron check to see if that date matches todays date? Would this work?

I'm not so sure on the syntax for this though, or if it is possible (I imagine it is though).

Thanks!
Rab
# 11  
Old 04-22-2010
Quote:
Originally Posted by rab
we require it to run on every fourth week.
You want to to run a weekly backup script every week except every fourth week of the year when you want to run a different script/command.
What about Smilie
Code:
if [ $(date +%u) -eq 5 -a $((($(date +%j) / 7) % 4)) -gt 0 ];then echo fourth week ;else echo regular week;fi

Edit:
Let's explain :
  • Code:
    $(date +%u) -eq 5

    if 5th day of the week(Friday) move to next condition.
  • Code:
    $((($(date +%j) / 7) % 4)) -gt 0

    calculate the week number from the beginning of the year.
  • Code:
    $((($(date +%j) / 7) % 4)) -gt 0

    divide the result by 4 and get the reminder, if the reminder is 0 the week is a fourth week.
If you run my code tomorrow (April 23, 2010) will be a fourth week Friday Smilie

Last edited by danmero; 04-22-2010 at 09:12 PM.. Reason: Add note
# 12  
Old 04-22-2010
I'll give it a try with some overly commented code.
Oh. And run it as a cron job every friday.

Code:
#!/bin/bash

# name of file to keep track of last backup
lastbackup=./lastbackup

if [ -f $lastbackup ]
then

# Time of last backup (seconds since epoch)
    timelast=$(stat -c %Y $lastbackup)

# Current time (seconds since epoch)
    timenow=$(date +%s)

# 86400 sec per day. 28 days is four weeks
    fourweeks=$(( 86400 * 28 ))

# slacktime = Some slack so we survive bad clock
# and daylight saving time. (2h)
    slacktime=$(( 3600 * 2 ))

# Have more than four weeks passed? (minus 2h)
    timepassed=$(( $timenow - $timelast ))
    fourweeksslack=$(( $fourweeks - $slacktime))

    if [ $timepassed -gt $fourweeksslack ]
    then
        echo BackupBackupBackup
    else
        echo NoBackup
    fi
else
# Should only occur first time script is run
    echo $lastbackup was created
    touch $lastbackup
fi

# 13  
Old 04-22-2010
Quote:
Originally Posted by danmero
You want to to run a weekly backup script every week except every fourth week of the year when you want to run a different script/command.
What about Smilie
Code:
if [ $(date +%u) -eq 5 -a $((($(date +%j) / 7) % 4)) -gt 0 ];then echo fourth week ;else echo regular week;fi

The logic of the test command does not agree with the echo statements. It will echo "fourth week" 3 out of 4 fridays.

Instead of "-gt 0", "-eq 3" should harmonize with the echo statements.

Regards,
Alister

Last edited by alister; 04-22-2010 at 10:38 PM..
# 14  
Old 04-23-2010
Quote:
Originally Posted by danmero
  • Code:
    $((($(date +%j) / 7) % 4)) -gt 0

    calculate the week number from the beginning of the year.
You can use the +%U format which gives the week number (as i posted above).
Code:
(( $(date +%U) % 4))

will return false 1/4 and true 3/4
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

2. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

4. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

5. Shell Programming and Scripting

cron to get executed on 2nd and 4th saturday of every month

Hi , i need to reboot a server during 2nd and 4th saturday every month. i have come up with the below cron 30 17 8-14 * * if ; then /rebootscript; fi # to reboot every second saturday 30 17 22-28 * * if ; then /rebootscript; fi # to reboot every fourth saturday I am wondering why it... (3 Replies)
Discussion started by: chidori
3 Replies

6. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 Replies

7. UNIX and Linux Applications

Help regarding a cron job

Hi, I need to write cron job, which tries to build a project. It would check out code from a repository and then will attempt to build it .The results of the build would show up on a web-page (a table with rows, one for each build, should get updated; a link to the detailed log file; color green... (3 Replies)
Discussion started by: sat411
3 Replies

8. UNIX for Dummies Questions & Answers

cron job

Hi, How to monitor whether a cron job is running or errored out..?other than checking the process using ps-aef how to enter the cron job which throws the output in 1 file and errors in other file. i remember it can be done using >1 and >2 ..but not sure.. any expert..please help!! (1 Reply)
Discussion started by: rujus
1 Replies

9. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

10. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies
Login or Register to Ask a Question