Can cron do this?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Can cron do this?
# 8  
Old 10-11-2003
Edit: This is version 2.0. One thing version 1.0 could not handle is running something every other week. This version handles that and it now requires my datecalc script. You may need to adjust the alias if you install datecalc somewhere other than /usr/local/bin.

To understand to even/odd stuff: datecalc -j can return a day number for a given month day and year. And datecalc -d can return a numeric day-of-week. Subtract the second from the first and you get the day number of the sunday that starts the week. Divide that by 7 and you get a week number. Now we just need to test that for even or odd.

And now the code...
Code:
#! /usr/bin/ksh
alias datecalc=/usr/local/bin/datecalc

#  weekselector ---  script to assist in scheduling cron jobs
#  Perderabo  Oct 11, 2003
#
#  example:
#  You want to run command1 on the first monday of each month and
#  you want to run command2 on the last monday of each month.
#  Just use this syntax:
#
#    0 0 * * 1 /usr/local/bin/weekselector 1st && command1
#    0 0 * * 1 /usr/local/bin/weekselector last && command2
#
#  Version 2.0  March 23, 2005
#
#  You can now use "even" or "odd" to run commands every other week.
#  Whether or not a week is "even" or "odd" is arbitrary, but if one week
#  is "odd", the next will be "even".  An "even" Monday will be in the same
#  week as an "even" Tuesday.

integer month day year first last leap

set -A mdy $(date "+%m %d %Y")
month=${mdy[0]}
day=${mdy[1]}
year=${mdy[2]}
#date "+%M %d %Y"  | read month day year

case $1 in
    even|Even|EVEN|odd|Odd|ODD)
            integer dow mjd fmjd wk
            dow=$(datecalc -d $year $month $day)
            mjd=$(datecalc -j $year $month $day)
            ((fmjd=mjd-dow))
            ((wk=fmjd/7))
            ((wk=wk+${#1}))
            ((wk/2*2 == wk))
            exit $?
            ;;
    1|1st|1ST|first|First|FIRST)    last=7  ;;
    2|2nd|2ND|second|Second|SECOND) last=14 ;;
    3|3rd|3RD|third|Third|THIRD)    last=21 ;;
    4|4th|4TH|fourth|Fourth|FOURTH) last=28 ;;
    last|Last|LAST)
            #                 ja fe ma ap ma jn jl ag se oc no de
            set -A mlength xx 31 28 31 30 31 30 31 31 30 31 30 31

            if ((month != 2)) ; then
                last=${mlength[month]}
            else
                leap=0
                if ((!(year%100))); then
                        ((!(year%400))) && leap=1
                else
                        ((!(year%4))) && leap=1
                fi

                last=28
                ((leap)) && last=29
            fi;;
    *) exit 1;;
esac
((first=last-6))

((first<=day && day<=last))
exit $?


Last edited by Perderabo; 03-23-2005 at 10:45 PM..
These 2 Users Gave Thanks to Perderabo For This Post:
# 9  
Old 10-12-2003
Incredible Perderabo!

Simply incredible and very helpful to others.

Warmest Regards, Neo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

4. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

5. UNIX for Dummies Questions & Answers

How are cron.allow and cron.deny read?

Hi, all! I was working on my Debian, minding my own business but then I wanted to see what happened if the same user was included on both cron.allow and cron.deny :p I would have bet that cron.deny was going to override cron.allow for security reasons, but my computer proved me wrong:... (3 Replies)
Discussion started by: pereyrax
3 Replies

6. Solaris

User entry in both cron.allow and cron.deny

Hello All, Anybody please help me to know ,what happens when a user having entry in both cron.allow and cron.deny files.Wheather the user will be able to access the crontab??? Thanks in advance Vaisakh (5 Replies)
Discussion started by: ksvaisakh
5 Replies

7. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

8. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies

9. AIX

AIX and cron logs filtering ?: /etc/cronlog.conf, /var/adm/cron/log

Hi, I can use 'crontabs –e' and do all the scheduling I like. However I would like to auto send myself just the cronjobs logs that fail. That is to say the PIDs that fail and the related lines with those PID’s only. (Not the full set of logs) Has anyone done this work? Or does an AIX 5.3 tool... (0 Replies)
Discussion started by: Keith Johnson
0 Replies

10. UNIX for Advanced & Expert Users

Cron : you are not authorized to use cron. Sorry.

Hi..... I have a problem with cron . I have stopped and restarted cron daemon and when i type crontab -e i receive the message: crontab: you are not authorized to use cron. Sorry. I am a root user id = ( uid=0(root) gid=1(other) ) into the etc/cond.d/cron.allow i have type root user and into... (1 Reply)
Discussion started by: tt155
1 Replies
Login or Register to Ask a Question