Bi-Weekly script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bi-Weekly script
# 1  
Old 06-05-2012
Bi-Weekly script

Hi,

I found the following biweekly script by searching google:

###############################################
why not schedule it weekly, cache the "date" of the last run to a
file, and then at the beginning of your shell script
compare the current date with the cached date and only approach to the
real code when they are two-week difference.
This logic should be very easy to implement in both cronjob and the
shell script.

Code:
#/bin/sh
 
BASEDIR=/path/to/cron/bin
CACHEFILE=$BASEDIR/lastrun.txt
TODAY=$(date +%F)
DAY14=$(date -d'-14 days' +%F)
 
if ! [ -s "$CACHEFILE" ]; then
date +%F > "$CACHEFILE"
else
LASTRUN=$(<"$CACHEFILE")
if [ "$DAY14" != "$LASTRUN" ]; then
echo "not scheduled"
exit
fi
echo "$TODAY" > "$CACHEFILE"
fi
 echo "program here"

####################################################
This line gives error when i use -x debug option, can someone please help me to fix this?
Quote:
+ date '-d-14 days' +%F
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]

Thanks
Sincerely,
RM
# 2  
Old 06-06-2012
The options you are trying to use with date aren't supported by all implementations. You can either install AT&T's AST tools which has a date command that does, or install the GNU date which also supports it. I personally would write it this way -- integer math is easier than trying to strip apart a human readable date and adjusting for month/year boundaries.

Code:
#!/usr/bin/env ksh                 
# should also work in bash if you prefer

now=$( date +%s )                                   # current time -- seconds past epoch
nrd=$(( (now - (now % 86400)) + (86400 * 14) ))     # two weeks from today (midnight) -- next run date
cache_file=$HOME/${0%%*/}.next                      # holds date that next run is allowed on
if [[ -f $cache_file ]]
then
    read ok2run <$cache_file
    if (( ok2run > now ))                           # next run date still in future
    then
        echo "abort: hasn't been too weeks; last execution was: $(tail -1 $cache_file)"
        exit 1
    fi
fi
printf "%s\n%s\n" $nrd "$(date)" >$cache_file       # save timestamp of next date ok and date for humans

# continue with rest of script
echo "good to go"       # just for testing/confirmation



It first gets the current time in seconds since the epoch (I believe most date commands support the +%s format, and someone will chime in here if I'm wrong). From there it computes the value of midnight two weeks out -- allowing the script to be run on the same day of the week (two weeks out), any time after midnight.

It uses a cache file to save the next valid execution timestamp, and a human readable string of the last time the command was executed. If there is no file it assumes that it is ok to run.

Hope this was at least some help.
This User Gave Thanks to agama For This Post:
# 3  
Old 06-06-2012
date +%s (seconds since the epoch) is also a GNU extra to the date command.

The simplest way is to work from data +%U (week number in the year). Using mathematics appropriate to your Shell, divide the number by 2 and take the remainder. This gives you 0=odd , 1=even .


When you get anomalies, please post what Operating System and version your are running and what Shell you are using.
These 2 Users Gave Thanks to methyl For This Post:
# 4  
Old 06-08-2012
agama, methyl - Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Schedule and Run By weekly shell script in cronjob

Hi All, How to schedule a shell script(script name- byweeklyreport.sh) it should run by weekly in corn job or is there any script have to write to check week and then run the above script. example-1st run March 06 2013 2nd run March 20 2013 3rd run April 3 2013... (13 Replies)
Discussion started by: krupasindhu18
13 Replies

2. Shell Programming and Scripting

Crontab running bi-weekly

Hi experts, Need your help to schedule the script(test.sh) bi-weekly in linux machine. script need to run at every alernate thursday at 9 am . Please help to run the same. (6 Replies)
Discussion started by: abhigrkist
6 Replies

3. Shell Programming and Scripting

Writing a script to run weekly/monthly - check for weekday or day-of-the-month

Hi all, I currently have a UNIX file maintenance script that runs daily as a cron job. Now I want to change the script and create functions/sub inside it that runs on a weekly or monthly basis. To run all the scripts' daily maintenance, I want to schedule it in cron as simply maint.sh... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. Shell Programming and Scripting

Problem in Weekly file Transfer script

I have made a script which transfers some files of the entire week , but the script fails when the next month is started. For e.g; if i run the script on 5th may , but i need to transfer files of its previous week which is from 24th April to 30th april ,the script fails, i have this loop in the... (2 Replies)
Discussion started by: vee_789
2 Replies

5. UNIX for Advanced & Expert Users

Scheduling bi-weekly through cron

Is there a way in AIX to schedule a script to run bi-weekly through cron? I have a script that needs to run every other Wednesday, and this is what I thought I had to enter in the crontab file: 00 08 * * 3/2 /home/user/user.script It didn't like that. It reports a syntax error. I'm almost... (5 Replies)
Discussion started by: LPT
5 Replies

6. Shell Programming and Scripting

Weekly statistics using while loop.

Hi people i have a situation here: my server generates a statistics text file every 30 mins. i have a script (txt2csv.sh) to convert all the statistics for the day into one csv file. Therefore i am doing a script to auto converts the text files into csv base on a weekly basis. the script is... (4 Replies)
Discussion started by: filthymonk
4 Replies
Login or Register to Ask a Question