Operating System - HP-UX
1845854 Members
3724 Online
110250 Solutions
New Discussion

Re: Checking for a certain time and date variable within a script

 
Mark Cook
Advisor

Checking for a certain time and date variable within a script

I have a script that checks sql durations and wanted to have the system page me within the hours of 8:00am and 17:00pm Monday through Friday only. Below is a portion of the code. If I take out the time/day checks and test it with simple echo statements, it appears to work. This is ran in the ksh shell. Any suggestions/help would be GREAT!
Thanks - Mark

TIME=`date +%H`
DAY=`date +%a`

if [ "${diff_t}" -ge "${CRITICAL_T}" -a "${diff_t}" -le `expr "${CRITICAL_T}" + "${RUN_F}"` -o $IS_FIRST
-eq 0 ]
then
echo "---> Info for session $session `date +%c`" >>$CRITICAL_FILE

#Check for day and time
if [[ $TIME -gt 08 && $TIME -lt 17 && $DAY != Sat && $DAY != Sun ]];
then
echo "Info for session $session, \"CRITICAL\"" |mail mark_page
else
:
fi
onstat -g ses $session >> $CRITICAL_FILE
onstat -u |grep " $session " >> $CRITICAL_FILE
onstat -g ntt |grep " $session " >> $CRITICAL_FILE
onstat -g ses |grep "$session " |read a1 b1 c1 d1 f1 g1 h1
if [ "$d1" -gt 0 ]; then
ps -fea | grep " $d1 " >>$CRITICAL_FILE
fi
echo "---> END\n\n" >>$CRITICAL_FILE
fi

"The prerequisite for action is the will and courage to be TRUTHFUL!"
12 REPLIES 12
someone_4
Honored Contributor

Re: Checking for a certain time and date variable within a script

Can you post the whole script?
I would like to use it if it does work.

thanks

Richard
Sachin Patel
Honored Contributor

Re: Checking for a certain time and date variable within a script

Hi Mark,
I have one script in perl does good job.

if ($Curr_Date > 15 && $Date < 5){
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}
if ($Curr_Date > 20 && $Date < 10){
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}
Is photography a hobby or another way to spend $
Timothy Czarnik
Esteemed Contributor

Re: Checking for a certain time and date variable within a script

Mark,

I'm wondering if the statement:

if [[ $TIME -gt 08

is hosing up because of the 08, rather than just reporting 8. This may be ugly, but can you check for each hour between 8 and 17? That is:

if [[ $TIME=08 || $TIME=09 || $TIME=10 ... $TIME=17 && $DAY != Sat && $DAY != Sun ]] (notice the || OR instead of && AND) and see if that works? I'm wondering if KSH is seeing 08 as a string and not a number, so that the -gt is throwing it off..

My $.02...

-Tim
Hey! Who turned out the lights!
Mark Cook
Advisor

Re: Checking for a certain time and date variable within a script

Thanks to everyone for their suggestions...Tim I understand what you are saying, but if I simply write a script like:

#!/bin/ksh

TIME=`date +%H`
DAY=`date +%a`

if [[ $TIME -gt 08 && $TIME -lt 17 && $DAY != Sat && $DAY != Sun ]];
then
echo "Hello"
else
:
fi
####
It works just fine...that is why I'm confused.
"The prerequisite for action is the will and courage to be TRUTHFUL!"
Timothy Czarnik
Esteemed Contributor

Re: Checking for a certain time and date variable within a script

Mark,

With -gt 08 and -lt 17, this would only run between 9:00 am and 4:59 pm. That is, the hours 8 and 17 are not inclusive. That is what you are shooting for?

-Tim
Hey! Who turned out the lights!
Mark Cook
Advisor

Re: Checking for a certain time and date variable within a script

Tim,
Good point, actually it should be -gt 07:59 and -lt 17:01. I guess that would actually get everything from 8 to 5. That still does not explain to me why our DBA was getting paged at 22:00 over the weekend. Thoughts...?
"The prerequisite for action is the will and courage to be TRUTHFUL!"
Timothy Czarnik
Esteemed Contributor

Re: Checking for a certain time and date variable within a script

Mark,

Actually, the date +%H is only going to give the hour as a 2 digit number. It would never be 07:59 or something like that. You would probably want

if [[ $TIME -gt 7 && $TIME -lt 18....

better yet,

if [[ $TIME -ge 8 && $TIME -lt 17 (this covers 8:00 AM - 4:59 PM)

Why would it page at 22:00 on a Weekend night? Still looking and thinking, but from what I can see, it couldn't... is this the only instance of this script running?

-Tim
Hey! Who turned out the lights!
Mark Cook
Advisor

Re: Checking for a certain time and date variable within a script

Tim,
Yep that makes much sense. When you ask "is this the only instance of this script running" what do you actually mean by that. I'm sorry to have to ask that. By the way, if it is easier for you, you can e-mail me at
cookm@fleishman.com
I really want to thank you for your efforts.
Mark
"The prerequisite for action is the will and courage to be TRUTHFUL!"
Timothy Czarnik
Esteemed Contributor

Re: Checking for a certain time and date variable within a script

Mark,

I'm wondering if there isn't another cron entry on this or another server that calls this same script (or one like it) that has the hour/day check mixed up, or a -gt where a -lt is needed...

From what I see, there is no way this action would be performed unless it is between 8 AM and 5 PM on a weekday.

Also, in the 'else' section of the if loop, does it perform a similar action? I'm not sure if you removed the else section to save space/typing, or if that is the way it actually exists in the script. If there is nothing in the 'else' section, you can just remove it.

-Tim
Hey! Who turned out the lights!
Mark Cook
Advisor

Re: Checking for a certain time and date variable within a script

Actually Tim, this is a stand alone process not ran through cron. We kick it off by
nohup ./sqlcapture > nohup.out &

Any then this guy just keeps running unless we make changes to the code. Then it is stoped and then restarted. There is much more to this script, but only the CRITICAL status is what he wants to be paged on. Do you think that because it is a continues running process, that this is why it is not working correctly? I'm grasping for straws now.
"The prerequisite for action is the will and courage to be TRUTHFUL!"
Timothy Czarnik
Esteemed Contributor

Re: Checking for a certain time and date variable within a script

Mark,

I sent you an email to the address you listed above.

Is this a newly generated script, or has it been running for some time without incident?

-Tim
Hey! Who turned out the lights!
John Poff
Honored Contributor

Re: Checking for a certain time and date variable within a script

Hello,

I don't know if it helps you any, but here is a bit of script I was just playing with after reading this thread. There is an argument for the 'date' command that will return the day of the week as a number (1-Mon, 2-Tue...7-Sun), so it allows you to check for the day of the week as a number instead of a string. I use this in a script that ejects a DDS tape daily each Monday through Friday morning after a backup, but leaves the tape alone on the weekend. Here is the script I was playing with:

#!/bin/sh

# date_test

while true
do
TIME=$(date +'%H')
DAY=$(date +'%u')

if [[ $TIME -gt 7 && $TIME -lt 17 && $DAY -lt 6 ]]
then
print "TIME to work: HOUR is $TIME and WKDAY is $DAY"
else
print "TIME to go home"
fi
sleep 60
done


JP