Alternate mondays to run the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate mondays to run the script
# 1  
Old 02-06-2014
Alternate mondays to run the script

I need to run a script every other monday (alternate mondays) , Can anyone has logic to put into timer script.

We are using below logic to run scripts every day
Code:
daywk_is=$(date "+%u")

if	[[ ${daywk_is} -eq 1 ]]

then

1=monday, 2=tuesday, 3=wed, 4=thr, 5=fri, 6=sat, 7=sun

can anyone have idea on this
# 2  
Old 02-06-2014
What's your system?
# 3  
Old 02-06-2014
The easiest would be to use a flag and the logic is:
Code:
if FLAG present
then
   remove FLAG
else
  execute code
  add FLAG
endif

...
Comments
Up to you to decide if the flag is added after execution of job or at first execution by cron ...
Just giving you the idea

Last edited by vbe; 02-06-2014 at 12:32 PM.. Reason: comments
# 4  
Old 02-06-2014
Some of you old timers may remember my datecalc script. It makes date arithemetic easy.

Code:
#! /bin/ksh

#    $
#    $
#    $ cal 1 1973
#        January 1973
#    Su Mo Tu We Th Fr Sa
#        1  2  3  4  5  6
#     7  8  9 10 11 12 13
#    14 15 16 17 18 19 20
#    21 22 23 24 25 26 27
#    28 29 30 31
#
#    $ ./datecalc -j 1973 1 1
#    41683
#    $
#
#  So now I know that modified julian day 41683 was a Monday in the
#  distant past.
MasterMonday=41683
alias datecalc=./datecalc

year=$1
month=$2
day=$3
today=$(datecalc -j $year $month $day)
if (( ((today-MasterMonday)%7) == 0 )) ; then
        echo today is Monday

        # nweeks is the number of weeks since MasterMonday
        ((nweeks=(today-MasterMonday)/7))
        if ((nweeks%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 to test it out.
Code:
$ cal
   February 2014
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

$ ./monday2 2014 2 3
today is Monday
this is an even Monday
$ ./monday2 2014 2 10
today is Monday
this is an odd Monday
$ ./monday2 2014 2 17
today is Monday
this is an even Monday
$ ./monday2 2014 2 24
today is Monday
this is an odd Monday
$ ./monday2 2014 2 4
today is not Monday
$ ./monday2 2014 2 2
today is not Monday
$
$

This User Gave Thanks to Perderabo For This Post:
# 5  
Old 02-06-2014
Your idea is good:
because each week has 7 (uneven) days, the day-of-the-week is alternating between even and uneven.
When you divide by 2 and take the remainder, it will alternate between 0 and 1
Code:
daywk_is=$(date "+%u")

if [[ $(( $daywk_is % 2 )) -eq 1 ]]
then

# 6  
Old 02-06-2014
Quote:
Originally Posted by MadeInGermany
Your idea is good:
because each week has 7 (uneven) days, the day-of-the-week is alternating between even and uneven.
When you divide by 2 and take the remainder, it will alternate between 0 and 1
Code:
daywk_is=$(date "+%u")

if [[ $(( $daywk_is % 2 )) -eq 1 ]]
then

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.

Last edited by Perderabo; 02-06-2014 at 04:35 PM..
# 7  
Old 02-06-2014
For mondays every other week try also
Code:
T=$(date +"%u%W")
[[ $((T % 2)) -eq 1 ]] && [[ $T -lt 200 ]] && echo OK || echo NOK

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