Operating System - HP-UX
1833777 Members
2356 Online
110063 Solutions
New Discussion

Re: How to calculate in a script to not page/email me between certain hours ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

How to calculate in a script to not page/email me between certain hours ?

Thru cron (every 5 mins), I have a script that monitors certain processes for failure but I dont want it to page me a night (10pm and 6am). How can check that. Basically I need to find out the $10PM and $6Am value.

if [ $HOUR > "$10PM" ] or [$HOUR < "$6AM" ];then
echo "DONT PAGE OR EMAIL " >> $LOG
exit 123
else
echo "Problem Daytime" |mailx -s "First, Lastname" pager@xyz.com
fi
good judgement comes from experience and experience comes from bad judgement.
8 REPLIES 8
Patrick Wallek
Honored Contributor
Solution

Re: How to calculate in a script to not page/email me between certain hours ?

This should do it for you (this assumes you are using /usr/bin/sh or /sbin/sh for the shell in this script)

HOUR=$(date +%H) # Get Hour in 24 hour time
if (( ${HOUR} < 6 || ${HOUR} >= 22 )) ; then
echo "DON'T PAGE OR EMAIL " >> $LOG
else
echo "Problem Daytime"|mailx -s "First, Lastname" pager@xyz.com
fi
James R. Ferguson
Acclaimed Contributor

Re: How to calculate in a script to not page/email me between certain hours ?

Hi Sammy:

Do something like this:

...
mytime=`date '+%H%M'`
if [ "$mytime" -eq 0600 -o "$mytime" -eq 2200 ]; then
...

Regards!

...JRF...
TwoProc
Honored Contributor

Re: How to calculate in a script to not page/email me between certain hours ?

#!/bin/ksh
integer myhour
myhour=`/usr/bin/date +%H`
if [ $myhour -lt 6 or $myhour -gt 22 ]
then
echo "DONT PAGE OR EMAIL " >> $LOG
exit 123
else
echo "Problem Daytime" |mailx -s "First, Lastname" pager@xyz.com
fi
We are the people our parents warned us about --Jimmy Buffett
Sammy_2
Super Advisor

Re: How to calculate in a script to not page/email me between certain hours ?

Perfect and quick answers. Thanks JRF and Pat. Both of you gave similar answers. I was trying for a more difficult solution by parsing output of date command. (see below).
I will put it in and see if all looks good.



##DATE=`date`
##HOUR=`echo $DATE|awk '{print $4}'|cut -d: -f1`
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: How to calculate in a script to not page/email me between certain hours ?

Thansk to John as well.
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: How to calculate in a script to not page/email me between certain hours ?

All 3 of you gave similiar but accurate answers.
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: How to calculate in a script to not page/email me between certain hours ?

Correction: Pat and John solution I was looking for . JRF, though accurate but you did not include in between hours . thanks
good judgement comes from experience and experience comes from bad judgement.
Raj D.
Honored Contributor

Re: How to calculate in a script to not page/email me between certain hours ?

Hi Sammy ,

You can do like this :

HOUR=`date +%H`
if [ $HOUR -le 6 ] || [ $HOUR -ge 22 ]
then
echo "DONT PAGE OR EMAIL " >> $LOG
else
echo "Problem Daytime" |mailx -s "First, Lastname" pager@xyz.com
fi
######################################

Cheers ,
Raj.


" If u think u can , If u think u cannot , - You are always Right . "