Operating System - HP-UX
1827909 Members
2462 Online
109971 Solutions
New Discussion

Days Validation in Script or via Cron

 
SOLVED
Go to solution
panchpan
Regular Advisor

Days Validation in Script or via Cron

Hello.

I have 4 scripts A.sh , B.sh, C.sh , D.sh and would like to run them on different calender days.

A.sh [ Every working day]
B.sh [ Every weekday (NOT on Weekend & Monthend) AND After A.sh is finished successfully]
C.sh [ Every Weekend OR monthend AND After A.sh is finished successfully]
D.sh [ Every weekend OR monthend AND after C.sh is finished successfully]


This task has to be managed via cron. I am thinking of putting below validations in A.sh and run A.sh from cron. At the end of each script I am generating a script.success file, Which could help to validate file dependency. Could you please help me in putting the syntax across for day validations in UNIX. Something like below must be the last lines in A.sh :

process...
touch A.success
IF [today is mon-thu and [ -f A.success] and ! fri or last working day of month]
then
run B.sh
FI
IF [ today is Friday or last working day of month AND [ -f A.success]
then
run C.sh
FI
IF [ today is Friday or last working day of month AND [ -f C.success]
then
run D.sh
FI


Please advice the piece of code to be placed in last lines of A.sh?

Many THANKS !!!
4 REPLIES 4
Dennis Handly
Acclaimed Contributor
Solution

Re: Days Validation in Script or via Cron

Your C.sh to D.sh requirement is simple, have C invoke D, if you want to do D right after C finishes.

You also need to define "working" day and "weekend". (For me, working is mon-fri and weekend is sat and sun.)

So to do what you have after each "IF":
dow=$(date +%u) # 1 - 7 mon - sun
if [ $dow -le 4 -a -f A.success -a ! last_work_day ]; then

if [ \( $dow -eq 5 -o last_work_day \) -a f A.success ]; then

if [ \( $dow -eq 5 -o last_work_day \) -a f C.success ]; then

Then write a function last_day_of_month that will compute it.

typeset -i month_day dom mm dow year
set -A month_day 31 28 31 30 31 30 31 31 30 31 30 31

function last_day_of_month {
echo "DoM: $dom"
echo "Dow: $dow"
echo "Mon: $mm"
echo "Year: $year"
if [ $mm -eq 2 ]; then # Feb
if (( year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) )); then
(( month_day[1] += 1 ))
fi
fi
echo "days in month ${month_day[mm - 1]}"

return $(( dom == month_day[mm - 1] )) # 1 for true
}

dom=$(date +%d)
mm=$(date +%m);
dow=$(date +%u) # 1 - 7 mon - sun
year=$(date +%Y)

last_day_of_month
ldm=$?

echo $ldm

if [ $dow -le 4 -a -f A.success -a $ldm -eq 1 ]; then
echo B.sh
fi
if [ \( $dow -eq 5 -o $ldm -eq 1 \) -a -f A.success ]; then
echo C.sh
touch C.success
fi

if [ \( $dow -eq 5 -o $ldm -eq 1 \) -a -f C.success ]; then
echo D.sh
fi
Steven E. Protter
Exalted Contributor

Re: Days Validation in Script or via Cron

Shalom,

You need some way to work your script with local holidays or it will malfunction.

Also note that the local work week is different in different countries. Sun-Thursday here, Sat-Wednesday in some of the surrounding countries.

Here area few links to programs that might help with date calculations.

http://hpux.ws/merijn/caljd-2.25.sh
http://hpux.ws/merijn/caljd-2.2.pl

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
panchpan
Regular Advisor

Re: Days Validation in Script or via Cron

Thank you Dennis - The code looks very easy to understand.

How about this function?
last_work_day

Have i missed it in first code?

Thank you!
Dennis Handly
Acclaimed Contributor

Re: Days Validation in Script or via Cron

>How about this function? last_work_day

I didn't know how you defined it nor when your holidays were. But for me, Friday and simplifying to no holidays:

# handle last work day
return $(( dow <= 5 && dom == month_day[mm - 1] ||
dow == 5 && dom >= month_day[mm - 1] - 2 )) # 1 for true

That's (Friday or before) and last day of month. OR Friday and within 2 days of last day.