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 like that
please give some example except PERL Script

Thanks all in advance

Regards,
Krupa

Man Page for crontab (linux Section 5) - The UNIX and Linux Forums

Hi,
Sorry actually i need example for this,that you have provided information is not enough.

Thanks
Krupa

Every week

0 0 * * 0 byweeklyreport.sh

CronBuddy - a crontab sandbox

Depending on the OS you are using, you may have a cron setup that works for every two weeks, but by using the date command in your script you could schedule it weekly and they choose whether to actually execute or not.

On many flavours of unix, you may find that

date +%j

gives the Julian date. Given that there are seven days in a week, you could schedule every Monday and then have a test for "Is today an even Julian date?" test at the top.

Some other flavours will give the week of the year, and it's a similar thing, but probably using Julian date is the best.

So an entry for cron:-

$ crontab -e
0 4 * * 1 /home/myuser/myscript

will run this every Monday at 4am. You could then code your script like this:-

#!/bin/ksh

juldate=`date +%j`
((jultest=$juldate/2))
((jultest=$juldate*2))

if [ $juldate -ne $jultest ]
then
   print "This is an odd week of the year."
   print "No actions performed."
   exit
fi

# I have confirmed it is an even numbered Monday, so proceed

blah blah blah.......

Of course, you could make the test -eq to say you want to ignore even numbered Mondays. The divide by two, multiply by two relys on odd numbers division getting the 0.5 truncated, so if the Julian date is 123, the half is only counted as 61, so double it again and you get 122. Test then finds that they are different.

Does this help?

Robin
Liverpool/Blackburn
UK

Had you taken the time to read that man page, you would have found an example exactly dealing with and solving your problem.

Hi,
Actually my Requirement is like this :-
I have shell script in kern shell ,have to run alternate week per month
example-today's date is 27 Mar 2013 and my script will run today then for the next time when it will run it should check 1st what was the last run date(27 Mar 2013) if it is matched 15days from current date then run else sent a mail with waning mgs.

i have a AWK command it is checking only within a month
Code:

date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-3;print}'

suppose i am checking 20days from current date it is give some negative value(-8)
ex-2013:03:-8

I hope you will understand my problem.
Advance thanks for giving me solution.

Thank you All.

Regards,
Krupa

---------- Post updated at 04:08 AM ---------- Previous update was at 04:05 AM ----------

i don't think you are reading properly my thread
please read carefully and replu me back

So, do you mean
A) Consistently alternate weeks, or
B) certain weeks of the month which could be:-

  1. First & third Monday each month
  2. Second & Fourth Monday each month
  3. First, third and any possible fifth Monday each month

With all of these, you have to consider that there will either be an occasional extra week between runs (case 1 & 2) or occasions where the run is on consecutive weeks (case 3)

If you are after A then I have suggested a solution which you have not replied to. Could you consider that and let me know if I have it right, or if not, why not and I will try to modify my suggestion.

If you are after B then let me know which and I'm sure we can work it out.

Yours faithfully,
Robin
Liverpool/Blackburn
UK

Hi Rabin,

Sorry for delay reply.
below is the output for your program that you have given.--
This is an odd week of the year.
No actions performed.

But this is my simple Requirement.
i have to run a shell script a day in alternate week.
suppose today's date is Wednesday 27-Mar-2013 today will run
and next run date will be on 10-April-2013(Wednesday) after 14days gap.

Here my problem is when i will run on 10-April-2013 ,i have to check last run date (here last run date is 27-Mar-2013) and 14 days gap if it is matched 14 days from current date then run the shell script else give any msg.

about your Point..

i am not checking any even or odd day because i don't need that
2nd it may any day like Monday to Friday (but it should be alternate week from day of run)
3rd First, third and any possible fifth Monday each month ... Not like this.

Please understand my requirement and give me some solution.

Best Regards,
Krupa

If it is every two weeks, regardless of which month it falls into what I discussed in post #3 of this thread. Set the test operator highlighted in read to be -eq if you want to run on the odd weeks to be used and schedule the script for the correct day with cron. If you want it at 3am for instance, you can choose from a cron entry like one of these:-

0 3 * * 0   /path/biweekly.sh      # Sunday
0 3 * * 1   /path/biweekly.sh      # Monday
0 3 * * 6   /path/biweekly.sh      # Saturday

The others are obvious.

I suppose that you could do it another way and have a reference file.

When you run the script, you could touch the file and therefore set the file time stamp, then with find you could test if it is old enough to run. Something like:-

#!/bin/ksh


if [ `find /var/my_app -name bi_weekly_flag -mtime +12|wc -l` -eq 0 ]
then
   print "This is too soon after the previous run."
   print "No actions performed."
   exit
fi

# Update reference file and proceed

touch /var/my_app/bi_weekly_flag

blah blah blah.......

Then schedule it to run on the required day each week. You will need to create the file for the first run. You can set a very old date as a timestamp to be sure:

touch -mt 200001010000 /var/my_app/bi_weekly_flag

I've left the find looking for files older that 12 days (-mtime +12) because if you run the script at the same time, the difference between the time of the touch command running might make it a bit pot-luck.

Do either of these help, or have I still missed the point?

Robin

I appreciated your solution .

find /var/my_app -name bi\_weekly_flag -mtime \+14|wc -l\` -eq 0

Thank you very much.

i have a small doubt about date-
How to get before 15 day�s date(28:Mar:2013 format) from current date.
I tried with below code:-
date +%d:%b:%Y|awk -vFS=":" -vOFS=":" '{$1=$1-30;print}'

it is giving correct result if both dates are coming within a month
problem:-
if before 15 day �s date is coming previous month the result is giving �Ve value.

Please send me this important solution.

Best Regards,
Krupa

Well there is something I'm just getting started with. Depending on your release, you may have an option on the date command of --date=STRING

I've done a bit of experimenting and you could do something like:-

date +%d:%b:%Y --date="today - 15 days"

Is this an option?

If not, some systems respect a time-zone beyond 24 hours.

#!/bin/ksh
RealTZ="$TZ"

((hoursback=15*24))
((hoursback=$hoursback-$offset))       # Set the offset according to your base timezone against GMT or UTC.

TZ="GMT${hoursback}BST"         # Okay, so I'm British, pick your own variation

date +%d:%b:%Y

TZ="$RealTZ"

This is not available on all systems, AIX is one that is.

Do either of these suggestions help?

Robin

I have changed accordingly as per your suggestion,it is working fine.

Thanks alot

Regards,
Krupa

1 Like

You are welcome. I'm still learning too and it's good to have a target to experiment with.

Robin