Alternate mondays to run the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate mondays to run the script
# 8  
Old 02-07-2014
If you want alternate Mondays, you need to pass two tests:-
  • Is it a Monday?
  • Is this an odd week of the year? (Or you may prefer to use the even weeks of course)
The first test can be checked easily with the value from date +%u or date +%A
For the other, use date +%W and test if it's odd like this:-
Code:
((odd_or_even=`date +%W`%2))

This will give the remainder so you can tell odd or even easily, then:-
Code:
if [ $odd_or_even -eq 0 ]
then
    : # This is an even week
else
    : # This is an odd week
fi

Schedule the job every day and these tests will handle your requirement. Of course, you have to consider how to run the script late if there has been an operational problem, perhaps with a FORCE flag.

You can therefore consolidate it all like this:-
Code:
if [ \( `date +%A` = Monday -a $((`date +%W`%2)) -eq 0 \) -o "$FORCE" = "Y" ]
then
   : # This is an even week Monday or forced run
else
   : # This is an odd week or not a Monday and not forced.
fi

I hope that this helps.



Robin
Liverpool/Blackburn
UK




I hope that this helps.

Last edited by rbatte1; 02-10-2014 at 06:22 AM.. Reason: Correcting comments
This User Gave Thanks to rbatte1 For This Post:
# 9  
Old 02-07-2014
Alternate Monday

Quote:
Originally Posted by Perderabo
Your code looks like it would run the script every Monday, Wednesday, Friday, and Sunday.

**edit**
Actually I was not exploiting the fact that weeks have an odd number of days. I converted days to weeks. Then I depended on every other week number being odd. I see your point though. I can simplify my code by expoiting the odd number of days per week.
Code:
#! /bin/ksh

alias datecalc=./datecalc

year=$1
month=$2
day=$3
if [[ $(datecalc -D $year $month $day) = "Monday" ]] ; then

        echo today is Monday

        today=$(datecalc -j $year $month $day)
        if ((today%2)) ; then
                echo this is an odd Monday
        else
                echo this is an even Monday
        fi
else
        echo today is not Monday
fi
exit 0

And testing it...
Code:
$ ./monday2b 2014 2 3
today is Monday
this is an odd Monday
$ ./monday2b 2014 2 10
today is Monday
this is an even Monday
$ ./monday2b 2014 2 17
today is Monday
this is an odd Monday
$ ./monday2b 2014 2 24
today is Monday
this is an even Monday

I have reversed which Monday's are considered even between the two scripts but that was arbritrary anyway.


If i want to run on Friday's, i have to put Friday there rght,
I need to run the scripts on alterante fridays not on EVEN or ODD Fridays..
For eaxmple, for Jan , I have to run the script on 1/3, 1/17, 1/31 and 2/14 ,
If you run the script on Odd fridays , it will run on 1/3, 1/17, 1/31,2/7 not on 2/14.

---------- Post updated at 03:23 PM ---------- Previous update was at 03:22 PM ----------

If i want to run on Friday's, i have to put Friday there rght,
I need to run the scripts on alterante fridays not on EVEN or ODD Fridays..
For eaxmple, for Jan , I have to run the script on 1/3, 1/17, 1/31 and 2/14 ,
If you run the script on Odd fridays , it will run on 1/3, 1/17, 1/31,2/7 not on 2/14.

---------- Post updated at 03:26 PM ---------- Previous update was at 03:23 PM ----------

If i want to run on Friday's, i have to put Friday there rght,
I need to run the scripts on alterante fridays not on EVEN or ODD Fridays..
For eaxmple, for Jan , I have to run the script on 1/3, 1/17, 1/31 and 2/14 ,
If you run the script on Odd fridays , it will run on 1/3, 1/17, 1/31,2/7 not on 2/14.
# 10  
Old 02-07-2014
Try it out. Even or odd in this sense has nothing to do with the day of the month. It is the number of weeks since a startpoint.

And yes, change Monday to Friday if you want it to run on Fridays.
This User Gave Thanks to Perderabo For This Post:
# 11  
Old 02-11-2014
Hi Perderabo,
I ran the above script, the log says as below, it doesnt find ./datecalc, this is perl server

Alternate_Friday.ksh[7]: ./datecalc: not found
today is not Tuesday
# 12  
Old 02-11-2014
Is my way in post 8 not suitable?

Please let me know.


Thanks,
Robin
# 13  
Old 02-11-2014
I used this below
Code:
alias datecalc=./datecalc


year=$1
month=$2
day=$3
if [[ $(datecalc -D $year $month $day) = "Tuesday" ]] ; then

        echo today is Tuesday

        today=$(datecalc -j $year $month $day)
        if ((today%2)) ; then
                echo this is an odd Friday
        else
                echo this is an even Friday
        fi
else
        echo today is not Tuesday
fi
exit 0

, Output says
Alternate_Friday.ksh[28]: ./datecalc: not found

Last edited by Perderabo; 02-11-2014 at 12:17 PM..
# 14  
Old 02-11-2014
Is datecalc in your current directory?

Do you have any queries for my suggestions?


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

BASH function to rename file to last mondays date

I'm trying to create a bash function that will allow me to rename a file name emails.txt to last monday's date renem () { d=`date -d last-monday` mv ~/emails.txt ~/myemails/$d } but i en up wit this error any suggestion mv: target `2014' is not a directory (3 Replies)
Discussion started by: robshal
3 Replies

3. Shell Programming and Scripting

Alternate ways to use SQL query in Shell Script

Hello, A couple weeks ago I got some great help from a few of you regarding reading an sql query into a shell script. That post can be viewed here. Thanks to all who posted. The issue I have now is everytime I run the query I get the following error SP2-0027: Input is too long (> 2499... (6 Replies)
Discussion started by: bbbngowc
6 Replies

4. Shell Programming and Scripting

Script fails to run properly when run from CRONTAB

Hello all, I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly. My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but... (12 Replies)
Discussion started by: rusman
12 Replies

5. Shell Programming and Scripting

Run shell script alternate week per month

Hi All, Requirement :- 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... (2 Replies)
Discussion started by: krupasindhu18
2 Replies

6. Shell Programming and Scripting

Alternate to SLEEP for EXPECT within BASH script?

Fairly new to the System Admin world, and this is my first post here, hoping to get some clarification. I am using a BASH script to automate some Logfile Archiving (into .tars). The actual logfiles are accessed through an SSH, so I have used the following EXPECT sub-script within my main BASH... (8 Replies)
Discussion started by: Goatfarmer03
8 Replies

7. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

8. 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

9. HP-UX

BAD SUPER BLOCK - Run fsck with alternate super block number

Error received when I tried to restore a blank disk with an 'auto recovery' DDS tape via HP-UX recovery system 2.0 onto a 1Gb SCSI. I assumed it would do the setup, wrong. Could someone tell me the procedure to initial disk for recovering files using cpio. The system is a HP-UX 9.04 version on a... (1 Reply)
Discussion started by: admin wanabee
1 Replies
Login or Register to Ask a Question