Looping in the shell script with help of script timer.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping in the shell script with help of script timer.
# 1  
Old 05-24-2012
Question Looping in the shell script with help of script timer.

Hello Experts-

We are facing some issues in the while loop script when we use the script time to decide whether to exist from the loop or continue.
Below is the script

HTML Code:
SrcExitLoop="FALSE"
Src_InitialStartTime=`date +%s`
Src_StartTime=`date +%s`
Src_NUM_ALERTS=0
TOTAL_ALERTS=`expr <SOME WAIT THRESHOLD.e.g.50> / <SOME ALERT SECONDS.e.g 10>`


while [[ ${SrcExitLoop} == "FALSE" ]]
do

"Some Oracle Query"

Src_CurrentTime=`date +%s`
Src_WaitTime=`expr ${Src_CurrentTime} - ${Src_StartTime}`
Src_TotalTime=`expr ${Src_CurrentTime} - ${Src_InitialStartTime}`

if [[ ${Src_WaitTime} -gt <SOME ALERT SECONDS.e.g 10> ]]
 then
  if [[ ${Src_TotalTime} -gt <SOME WAIT THRESHOLD.e.g.50> ]]
   then
    echo "Reached Max Seconds"
  else
   Src_NUM_ALERTS=`expr ${Src_NUM_ALERTS} + 1`
   TotalTimeFormat=`printf ""%dh:%dm:%ds"\n" $((${Src_TotalTime}/3600)) $((${Src_TotalTime}%3600/60)) $((${Src_TotalTime}%60))`
   AlertSecondsFormat=`printf ""%dh:%dm:%ds"\n" $((${alert_seconds}/3600)) $((${alert_seconds}%3600/60)) $((${alert_seconds}%60))`
   errorSubject="ALERT ${Src_NUM_ALERTS}/${TOTAL_ALERTS} :- Process continues"
   errorText="EMAIL BODY"
   echo "${errorText}" | mailx -s "${errorSubject}" ${EMAIL_LIST}
  Src_StartTime=`date +%s`
 fi
else
  sleep <SOME VALUE.e.g.5>
fi

if [[ ${Src_TotalTime} -gt <SOME WAIT THRESHOLD.e.g.50> ]]
  then
    Src_NUM_ALERTS_1=`expr ${Src_NUM_ALERTS} + 1 `
    TotalTimeFormat=`printf ""%dh:%dm:%ds"\n" $((${Src_TotalTime}/3600)) $((${Src_TotalTime}%3600/60)) $((${Src_TotalTime}%60))`
    AlertSecondsFormat=`printf ""%dh:%dm:%ds"\n" $((${alert_seconds}/3600)) $((${alert_seconds}%3600/60)) $((${alert_seconds}%60))`
    errorSubject="ALERT ${Src_NUM_ALERTS_1}/${TOTAL_ALERTS} :- Process STOPPED"
    errorText="EMAIL BODY"
    echo "${errorText}" | mailx -s "${errorSubject}" ${EMAIL_LIST}
    SrcExitLoop="TRUE"
    exit 0
fi
done
This script works fine most of the time. Since we are using unix start and end time to determine the wait time and total time, it some times doesn't give all the emails.Smilie
This happens because the return time from the oracle is more.Smilie

For example :-
if our sleep time is 5 seconds ,
alert time is 10 seconds,
total threshold is 50 seconds.
Ideally we should receive 5 email every 10 seconds. Subject will "ALETRT 1/5...5/5".
But some times oracle is taking more than 10 seconds to return the value. Now the Src_NUM_ALERTS variable skips the one value..so insteading of receving 5 emails we receive only 4 emails and last email is "ALERT 4/5".
It miss one iteration that is breaks at...
Code:
Src_NUM_ALERTS=`expr ${Src_NUM_ALERTS} + 1`

Is there any other way where in we can avoid this skip? Smilie Can we simply use counter?Smilie
How can we define in the script that alert us every pre defined seconds(e.g.10 seconds) and do iteration depending upon the total wait time divided by total alert time(e.g.50/10)?Smilie
The email won't be coming exactly after 10 seconds(in this case) but we should recevie 5 emails(for above parametrers).
Please help.


Thanks in advance!!
# 2  
Old 05-24-2012
You're never going to get it exactly 50.00000000000000 seconds long. Anything you do could throw it off just slightly. Make your threshold slightly higher than 50 if you want all the emails sent.

Also, you can put the query in the background before the loop, and wait for it after, so things don't wait for each other needlessly.

Code:
while condition
do
        query &

        ...

        wait
done

# 3  
Old 05-25-2012
I think the email part you should put in another script. Call it in background by passing variables as argument.

hope this should work -

Code:
SrcExitLoop="FALSE"
Src_InitialStartTime=`date +%s`
Src_StartTime=`date +%s`
Src_NUM_ALERTS=0
TOTAL_ALERTS=`expr <SOME WAIT THRESHOLD.e.g.50> / <SOME ALERT SECONDS.e.g 10>`


while [[ ${SrcExitLoop} == "FALSE" ]]
do

"Some Oracle Query"

Src_CurrentTime=`date +%s`
Src_WaitTime=`expr ${Src_CurrentTime} - ${Src_StartTime}`
Src_TotalTime=`expr ${Src_CurrentTime} - ${Src_InitialStartTime}`

if [[ ${Src_WaitTime} -gt <SOME ALERT SECONDS.e.g 10> ]]
 then
  if [[ ${Src_TotalTime} -gt <SOME WAIT THRESHOLD.e.g.50> ]]
   then
    echo "Reached Max Seconds"
  else
   Src_NUM_ALERTS=`expr ${Src_NUM_ALERTS} + 1`
   TotalTimeFormat=`printf ""%dh:%dm:%ds"\n" $((${Src_TotalTime}/3600)) $((${Src_TotalTime}%3600/60)) $((${Src_TotalTime}%60))`
   AlertSecondsFormat=`printf ""%dh:%dm:%ds"\n" $((${alert_seconds}/3600)) $((${alert_seconds}%3600/60)) $((${alert_seconds}%60))`
   errorSubject="ALERT ${Src_NUM_ALERTS}/${TOTAL_ALERTS} :- Process continues"
   errorText="EMAIL BODY"

   nohup sendAlertMail.ksh "$errorText" "$errorSubject" "$EMAIL_LIST" 2>&1 &

   #echo "${errorText}" | mailx -s "${errorSubject}" ${EMAIL_LIST}

  Src_StartTime=`date +%s`
 fi
else
  sleep <SOME VALUE.e.g.5>
fi

if [[ ${Src_TotalTime} -gt <SOME WAIT THRESHOLD.e.g.50> ]]
  then
    Src_NUM_ALERTS_1=`expr ${Src_NUM_ALERTS} + 1 `
    TotalTimeFormat=`printf ""%dh:%dm:%ds"\n" $((${Src_TotalTime}/3600)) $((${Src_TotalTime}%3600/60)) $((${Src_TotalTime}%60))`
    AlertSecondsFormat=`printf ""%dh:%dm:%ds"\n" $((${alert_seconds}/3600)) $((${alert_seconds}%3600/60)) $((${alert_seconds}%60))`
    errorSubject="ALERT ${Src_NUM_ALERTS_1}/${TOTAL_ALERTS} :- Process STOPPED"
    errorText="EMAIL BODY"
    
     nohup sendAlertMail.ksh "$errorText" "$errorSubject" "$EMAIL_LIST" 2>&1 &

     #echo "${errorText}" | mailx -s "${errorSubject}" ${EMAIL_LIST}

    SrcExitLoop="TRUE"
    exit 0
fi
done

the script sendAlertMail.ksh will be like this or u can modify as per ur requirements

Code:
#! /bin/ksh
echo "$1" | mailx -s "$2" "$3"

# 4  
Old 05-25-2012
CPU & Memory

Thanks all for your reply!!Smilie

I have tried to put the query and email command in no hup but still the some times we are missing one email.

Again there is no need to recevie the email on exact time...but we have to recevie all the emails...I think I have to modify the script (use counter) to get all the emails. Smilie

Thoughts??

Thanks again for your help!
# 5  
Old 05-25-2012
put THRESHOLD time to 59. I this case it will not send the mail 6 times but chances are there that 5 mails can be sent.

also try putting the if and else part in separate file with mail option and call it in background with passing argument. will take less of your looping time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping not completing in shell script

Hi, Iam using below code to login to servers to get cpu utilisation. but output is coming for only one server. code is below root@blr-svr-oclan-01 # more SSSC_CPU_UTIL1.sh #!/bin/sh echo "CPU UTILIZATION" while read line; do IDLE=`/usr/local/bin/sshpass -p 'xxx' ssh xxx@$line 'sar 2 2' |... (1 Reply)
Discussion started by: surender reddy
1 Replies

2. UNIX for Dummies Questions & Answers

Shell Script / Timer

Hey folks, We already got a working Script running, but actually we gotta start it manually but at least we want to run it like every 30minutes 1time. Could anyone give it a shot? #!/bin/sh for i in A B C D E F G H I J K L M N O P Q R S T U V W X Z do ln -sv /x/$i/*/PDF/001-*.*... (1 Reply)
Discussion started by: Yjerith
1 Replies

3. Shell Programming and Scripting

C Shell Script: While function not fully looping

I am new to scripting and this is probably the 4th or 5th simple script I have written. I am working with a HUGE number of data that need to be organized into folders and named a certain way. I wrote the naming script using a while function to go through the 1000-some folders and rename the files... (0 Replies)
Discussion started by: notluckyhannah
0 Replies

4. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

5. Shell Programming and Scripting

60 second Timer with Shell Script

Please how can i display a 60 second active countdown timer in an echo message. (7 Replies)
Discussion started by: frankycool
7 Replies

6. Shell Programming and Scripting

Simple script? First timer...

Hey guys, I'm brand new to the bash scripting world, here's my issue.. When people SSH to my box, I'm wanting to use screen to monitor them if I so choose. So, I've created a new user: testing, and I've set its startup shell to /bin/myshell.sh whose contents read this: #!/bin/sh $me =... (4 Replies)
Discussion started by: poon
4 Replies

7. Shell Programming and Scripting

Convert shell script for looping

Situation: I have a working shell script on our file server (OSXS Tiger) to connect to a workstation, which is using a portable home directory (phd), and rsync a user's MirrorAgent.log. I'm not that strong of a scripter (obviously), but I would like to add other workstations to this script as they... (4 Replies)
Discussion started by: le0pard13
4 Replies

8. Shell Programming and Scripting

looping through a variable in a shell script

hi, my first question is :- i would like to know how do i loop through the output of a variable. for ex:- if i have a variable called x and echo $x gives the output like feb 19 07 feb 20 07 feb 21 07 i would like to know how do i loop through this since it is separated and i... (1 Reply)
Discussion started by: ramachandranrr
1 Replies

9. UNIX for Dummies Questions & Answers

script run timer

any of you guys have a script for measuring the run time of any given script, where you would sandwich your own script between the timer script or something? thanks! :) (2 Replies)
Discussion started by: mark_nsx
2 Replies

10. Shell Programming and Scripting

Looping a perl script in a shell script

I am trying to get the follow script to run in the background on the 'fly'. I can launch it via cron and it will run in the background. BUT when I launch it from the command line it will run in the foreground. I figure it has to do with the while loop I have, but I have no clue how I can run the... (8 Replies)
Discussion started by: edkung
8 Replies
Login or Register to Ask a Question