Schedule and Run By weekly shell script in cronjob


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Schedule and Run By weekly shell script in cronjob
# 8  
Old 03-26-2013
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
# 9  
Old 03-27-2013
Schedule and Run By weekly shell script in cronjob

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
# 10  
Old 03-27-2013
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:-
Code:
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:-
Code:
#!/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:
Code:
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
# 11  
Old 03-28-2013
get before 15 day’s date from current date

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

Last edited by krupasindhu18; 03-28-2013 at 05:15 AM..
# 12  
Old 03-28-2013
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:-
Code:
date +%d:%b:%Y --date="today - 15 days"

Is this an option?

If not, some systems respect a time-zone beyond 24 hours.
Code:
#!/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
# 13  
Old 03-29-2013
I have changed accordingly as per your suggestion,it is working fine.

Thanks alot

Regards,
Krupa
This User Gave Thanks to krupasindhu18 For This Post:
# 14  
Old 04-02-2013
You are welcome. I'm still learning too and it's good to have a target to experiment with.


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script does not run from a user specific cronjob.

Hello, I have two crontabs, one for the root and one for another user. There is a script in my configurations that has to send a email. The script works and sends the emails when I run it by hand with either the root or the user, and when I program it in the root's crontab. But! It does not... (3 Replies)
Discussion started by: Tralaraloro
3 Replies

2. UNIX for Dummies Questions & Answers

Best way to schedule script to run Ubuntu 10.04

On Ubuntu 10.04 LTS, I would like to know the best way to schedule myscript.sh to run at a specified time, please provide examples and specify things like does cron have to be running, how do I check if cron is running and all that. I have tried unsuccessfully in the past to run the AT command,... (1 Reply)
Discussion started by: glev2005
1 Replies

3. Shell Programming and Scripting

Schedule a script to run at 10am from mon to fri

Hi, I need to write a shell script which will run i background and will execute other script only on Mon to Fri 10 AM but not on Sat and Sun. I am able to set it to run on every day at 10AM but how to make it to run only on Mon to Fri Thanks, Firestar. (7 Replies)
Discussion started by: firestar
7 Replies

4. AIX

My script didn't run every run every minute at cronjob

In my cronjob, I would like to schedule my script.sh to run every minutes. I crontab -e and have in line below but it didn't seems to run at all. * * * * * script.sh When I run it manually, I can run it. Is that anything wrong with the above line? If I change it to something like below,... (4 Replies)
Discussion started by: ngaisteve1
4 Replies

5. Shell Programming and Scripting

How to schedule a cronjob to run every 15 mins ?

Hi, I want to schedule a job to run every 15 mins through cron. searched the forums and came up with this piece of code.i have given this in my crontab 0-59/15 * * * * sh /usr/ss/job But its not being run. Have i made any mistake here. Can any1 post the cron code for scheduling the... (5 Replies)
Discussion started by: suresh_kb211
5 Replies

6. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

7. Shell Programming and Scripting

Running script that sends an html formatted email fails when its run as cronjob

Hi Im very new at working with unix and this problem I simply can not understand. I know there are a lot of threads about problems with shell scripts behaving differently when run from a terminal and from a cronjob. I have tried everything(almost) but I still havent cracked this problem. Im... (15 Replies)
Discussion started by: Nightowl
15 Replies

8. UNIX for Dummies Questions & Answers

cronjob to run perl script

Hi all Recently i had finished a perl script. When i run manually, the script work fine. But when i wanted to put the script in cron, it didn't get the same output as it run manually. I felt that it only execute the script until certain line then it stop as i see most of the related files didn't... (6 Replies)
Discussion started by: AirWalker83
6 Replies

9. Solaris

How to run a script as different user inside cronjob in solaris.

Hi , I have a shell script to perform some actions on sun solaris box . This script normally requires to be run as a different user. so, whenever i have to run this script, i need to sudo in as that user , enter the password and execute it. Now,I have to setup a cronjob to execute the script... (11 Replies)
Discussion started by: csg_user
11 Replies

10. Shell Programming and Scripting

set schedule to run a script at background while logout

Hi, How can I run a script at 9:00am and 6:00pm everyday? Can I run it at background while I logout my account? Please help!! Many Thanks!! (1 Reply)
Discussion started by: happyv
1 Replies
Login or Register to Ask a Question