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]
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.
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:
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)
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)
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)
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)
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)
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)