Operating System - HP-UX
1755406 Members
2905 Online
108832 Solutions
New Discussion юеВ

Time parameter in a script

 
SOLVED
Go to solution
Ragni Singh
Super Advisor

Time parameter in a script

Hey guys, I need some help here. How can I add a time parameter in a script that does the following. I'd like for the script to check if the time is 5:05, it will execute the script and return a 0 code and the end time of 1:15 pm.

I hope you understand what I'm asking for. Any help is greatly appreciated and points will be assigned.
10 REPLIES 10
harry d brown jr
Honored Contributor

Re: Time parameter in a script

Do a man on "cron" and "crontab" adding an entry for

05 05 * * * /scriptpath/scriptname

then one for 1:15pm

15 13 * * * /scriptpath/scriptname


live free or die
harry
Live Free or Die
Ragni Singh
Super Advisor

Re: Time parameter in a script

Thanks for your response but here is the whole issue. YOu see we have a scheduled template that monitors some processes that run from 5:05 till 1315 only. In openview template, you can't specify from 05-1315. You can give it like 0-59 for minutes and then 1-24 for hours. If I specify 5-59, that would mean that the first 5 minutes of every hour be skipped. I hope I haven't confused you so far. Thats when I thought I could just add a time check that checks for something like such... if 5:05 run script, if time is past 1315, stop script. I think I can try adding this in cron and see how that works out. Thanks.
A. Clay Stephenson
Acclaimed Contributor

Re: Time parameter in a script

Here's one way:

#!/usr/bin/sh

calc_time() # converts hrs and mins to minutes since midnite
{
typeset -i10 HR=${1}
typeset -i10 MIN=${2}
shift 2
typeset -i10 TM=$(( (${HR} * 60) + ${MIN} ))
echo "${TM}"
return 0
} # calc_time

START=$(calc_time 5 5)
END=$(calc_time 13 15)
NOW=$(calc_time $(date '+%H %M'))
if [[ ${NOW} -ge ${START} && ${NOW} -le ${END} ]]
then
echo "Do your stuff; your commands go here".
fi
If it ain't broke, I can fix that.
Ragni Singh
Super Advisor

Re: Time parameter in a script

Thanks for your help Clay but could you please explain to me what those 3 typeset paramters are?
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Time parameter in a script

Typesets are the shell's way of 'typing' variables. You can leave the typesets out and the script will still work but it is considered good coding practice to always typeset shell variables.

typeset -i10 XX=30 declares XX to be a base 10 integer variable. Integer typesets become very important in some shells when leading zeroes may occur.

Man sh-posix for details.

If it ain't broke, I can fix that.
Ragni Singh
Super Advisor

Re: Time parameter in a script

Here goes my script. It complains about some syntax error and I believe it has to do with the if, do statments. Can anyone point out to me what its complaining about.

1 #!/usr/bin/ksh
2
3 OPCMSG=/opt/OV/bin/OpC/opcmsg
4 PROC[1]="processManager"
5 PROC[2]="fbam"
6 PROC[3]="pcxsml"
7 PROC[4]="fbdm"
8 PROC[5]="fbpoetio"
9 PROC[6]="fbneonio"
10 PROC[7]="pcxrm"
11 PROC[8]="fbmoc"
12 PROC[9]="fbdbx"
13
14 calc_time() # converts hrs and mins since midnite
15 {
16 typeset -i10 HR=${1}
17 typeset -i10 MIN=${2}
18 shift 2
19 typeset -i10 TM=$(( (${HR}*60) + ${MIN} ))
20 echo "${TM}"
21 return 0
22 } # calc_time
23 START=$(calc_time 5 5)
24 END=$(calc_time 13 15)
25 NOW=$(calc_time $(date '+%H %M'))
26 if [[ ${NOW} -ge ${START} && ${NOW} -le ${END} ]]
27 then
28 ps -ef | grep -v grep | egrep '(processManager|fb[ad]m|pcxsml|fb.*io|pcx
rm|fbmoc|fbdbx)' | awk '{ print $8 }' | sed 's/\/..*\///g' > /var/tmp/fbhh_proc
29
30 for i in 1 2 3 4 5 6 7 8 9
31 do
32
33 grep -q ${PROC[i]} /var/tmp/fbhh_proc
34 if [ $? -ne 0 ]
35 then
36 ${OPCMSG} appl=FBHH s=critical msg_g=FBHH object=process \
37 msg_t="PROCESS ${PROC[i]} IS NOT WORKING!!!"
38 fi
39
40 done

Vitek Pepas
Valued Contributor

Re: Time parameter in a script

You are missing 'fi' statement for 'if' in line 27.
A. Clay Stephenson
Acclaimed Contributor

Re: Time parameter in a script

Here is one obvious error:

grep -q ${PROC[i]} /var/tmp/fbhh_proc

should be

grep -q ${PROC[${i}]} /var/tmp/fbhh_proc

I also never saw where PROC was ever assigned a value.
If it ain't broke, I can fix that.
Vitek Pepas
Valued Contributor

Re: Time parameter in a script

Correction: 'if' is in line 26, missing 'fi' should be in line 29.