Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am working on a project in which I need to make a url call to one of my server from the bash shell script..

http://hostname.domain.com:8080/beat

After hitting the above url, I will be getting the below response which I need to parse it and extract value of syncs and syncs_behind

state: READY process: 30 process_behind: 100 num_rounds: 60 hour_col: 2 day_col: 0 oldest_day_col: 0

Now I need to hit the above url every 10 seconds for a period of 10 minutes and extract the value of process and process_behind from it and then use it to validate it with below conditions -

process > 8
process_behind = 0

if the syncs is greater than 8 and process_behind = 0, then I will end my shell script with some message saying - "Data has been validated", otherwise I would keep on trying for 10 minute window.. If in that 10 minute window, the above condition doesn't met I will end the shell script anyway meaning I won't retry again.

Below is my shell script which does the above thing and it works fine in normal condition when the server is up.

#!/bin/bash

COUNT=60   #number of 10 second timeouts in 10 minutes

while [[ $COUNT -ge "0" ]]; do

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for process and process_behind
PROCESS=$(echo $DATA | grep -oE 'process: [0-9]+' | awk '{print $2}')
PROCESS_BEHIND=$(echo $DATA | grep -oE 'process_behind: [0-9]+' | awk '{print $2}')

echo $PROCESS
echo $PROCESS_BEHIND

#verify conditionals
if [[ $PROCESS -gt "8" && $PROCESS_BEHIND -eq "0" ]]; then exit 0; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

There might be a scenario in which it gets failed suppose if the server is down, then wget line throws an exception.

Now what I am trying to do is, if the server is down, then I will sleep for 30 seconds and then retry executing the server url again and if it failed again, then sleep again for 30 seconds and then retry executing the server url again. I will retry executing the server url for n times, let's say n is 10.

And after that still the server is not up I will exit out of the shell script with non zero status and a message server is down. But if the server is up and I am able to get the response back, I will move forward to extract those fields which I have in my later shell script.

Is this possible to implement retry mechanism in bash shell script? Or is there any better way of doing this apart from wget?

UPDATE 1:-

This is what I have got -

#!/bin/bash

COUNT=60   #number of 10 second timeouts in 10 minutes
DATA=""
RETRY=10

while [[ $COUNT -ge "0" ]]; do

while [ $RETRY -gt 0 ]
do
    #send the request, put response in variable
    DATA=$(wget -O - -q -t 1 http://machineA:8080/beat)
    echo "Hello"
    if [ $? -eq 0 ]
    then
        break
    else
        let RETRY-=1
        sleep 30
    fi
done

if [ $RETRY -eq 0 ]
then
    exit 2
fi

#grep $DATA for process and process_behind
PROCESS=$(echo $DATA | grep -oE 'process: [0-9]+' | awk '{print $2}')
PROCESS_BEHIND=$(echo $DATA | grep -oE 'process_behind: [0-9]+' | awk '{print $2}')

echo $PROCESS
echo $PROCESS_BEHIND

#verify conditionals
if [[ $PROCESS -gt "8" && $PROCESS_BEHIND -eq "0" ]]; then exit 0; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

If my server is down and I am running the above shell script, then it is printing "Hello" to the console and it works fine. But see my below update -

Update 2:-

Ok, Now I have found the issue, if I am running the shell script like this which I will be running in my production system and if the server is down, then it doesn't prints out "Hello" at all. But if I run the above shell script in Update 1 and if the server is down, then it works fine.

#!/bin/bash

COUNT=60   #number of 10 second timeouts in 10 minutes
HOSTNAME=machineA
DATA=""
RETRY=10

while [[ $COUNT -ge "0" ]]; do

while [ $RETRY -gt 0 ]
do
    #send the request, put response in variable
    DATA=$(wget -O - -q -t 1 http://$HOSTNAME:8080/beat)
    echo "Hello"
    if [ $? -eq 0 ]
    then
        break
    else
        let RETRY-=1
        sleep 30
    fi
done

if [ $RETRY -eq 0 ]
then
    exit 2
fi

#grep $DATA for process and process_behind
PROCESS=$(echo $DATA | grep -oE 'process: [0-9]+' | awk '{print $2}')
PROCESS_BEHIND=$(echo $DATA | grep -oE 'process_behind: [0-9]+' | awk '{print $2}')

echo $PROCESS
echo $PROCESS_BEHIND

#verify conditionals
if [[ $PROCESS -gt "8" && $PROCESS_BEHIND -eq "0" ]]; then exit 0; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

This is what I am getting in the debug mode with the above script -

david@some-machine:~$ bash -x ./ping1.sh
+ set -e
+ COUNT=60
+ HOSTNAME=machineA
+ DATA=
+ RETRY=10
+ echo machineA
machineA
+ [[ 60 -ge 0 ]]
+ '[' 10 -gt 0 ']'
++ wget -O - -q -t 1 http://machineA:8080/beat
+ DATA=

I guess both of the scripts are same? Then why this is happening?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use a simple loop:

....
DATA=""
RETRY=10

while [ $RETRY -gt 0 ]
do
    DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)
    if [ $? -eq 0 ]
    then
        break
    else
        let RETRY-=1
        sleep 30
    fi
done

if [ $RETRY -eq 0 ]
then
    exit 2
fi
....
share|improve this answer
    
Thanks for the help. I have one confusion with this, why do you have last if check like this if [ $RETRY -eq 0 ]? –  Webby Apr 26 at 5:18
    
In my case, I just tested out, if the server id down then it doesn't go to next line after this DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat) somehow. I just added echo "Hello" after the above line and it doesn't gets printed out if the server is down. Any thoughts? –  Webby Apr 26 at 15:43
    
@Webby: The last if check is for the case when server is up after n times attemps (n <= 10). Example, for the 5th check, the server is up, so we quit the while loop. In this case, $RETRY is greater than 0, so we continue the script. If $RETRY equal 0, meaning that the server is still down, so we quit the script. –  Gnouc Apr 26 at 18:44
    
I guess last if check is for the case when the server is still down after 10 attempts? Right not for the case when it is up. –  Webby Apr 26 at 19:31
    
I have updated the question with the details. Somehow "Hello" doesn't gets printed out if the server is down. –  Webby Apr 26 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.