Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

is OR correct with the two statements in until loop?

21 views
Skip to first unread message

mike

unread,
Jun 5, 2018, 10:17:51 AM6/5/18
to
Hi,

I have the following "bash script" for checking if the webservice for our
sonarQube is up and running.


I test it with restarting our service and I never get a
"sonar webservice is up and running!" instead I get "Timed out! sonar webservice is not running"

Any ideas what I am missing?

//mike

max_repeat=40;
repeat=0;

webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]
do
printf '.'
sleep 6
((repeat++))
done

if [ "$repeat" -eq "$max_repeat" ];
then
echo "Timed out! sonar webservice is not running"
exit 1;
else
echo "sonar webservice is up and running!"
fi



Kenny McCormack

unread,
Jun 5, 2018, 10:49:21 AM6/5/18
to
In article <73cc794e-4eaa-4d28...@googlegroups.com>,
mike <mikaelp...@hotmail.com> wrote:
>Hi,
>
>I have the following "bash script" for checking if the webservice for our
>sonarQube is up and running.
>
>
>I test it with restarting our service and I never get a
>"sonar webservice is up and running!" instead I get "Timed out! sonar webservice
>is not running"

The main thing is that you have to run the curl command inside the loop,
not just once before the loop.

Something like this should work:

for ((i=0; i<$maxtries; i++))
do
curl ... && break
done
[ $i = $maxtries ] && echo Failed || echo Worked

--
Watching ConservaLoons playing with statistics and facts is like watching a
newborn play with a computer. Endlessly amusing, but totally unproductive.

hymie!

unread,
Jun 5, 2018, 10:52:47 AM6/5/18
to
In our last episode, the evil Dr. Lacto had captured our hero,
mike <mikaelp...@hotmail.com>, who said:

> webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
> until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]
> do
> printf '.'
> sleep 6
> ((repeat++))
> done

The first problem I see is that you are only making one test, and then
you are checking 40 times to see if that test succeeded. Apparently
the test failed.

The other question is ... do you know exactly what is in the
$webservice_up variable and/or the results of your curl command?
For example, are you sure the response code is 200?
(Without access to your web server or the specific URL that you are
contacting, I can't answer that question for you.)

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net

mike

unread,
Jun 5, 2018, 11:08:18 AM6/5/18
to
ok so [ $webservice ] will not execute the command:


$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')

?

mike

unread,
Jun 5, 2018, 11:11:04 AM6/5/18
to
Den tisdag 5 juni 2018 kl. 16:52:47 UTC+2 skrev hymie!:
> In our last episode, the evil Dr. Lacto had captured our hero,
> mike <mikaelp...@hotmail.com>, who said:
>
> > webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
> > until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]
> > do
> > printf '.'
> > sleep 6
> > ((repeat++))
> > done
>
> The first problem I see is that you are only making one test, and then
> you are checking 40 times to see if that test succeeded. Apparently
> the test failed.


I thought this would test twice:


until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]


>
> The other question is ... do you know exactly what is in the
> $webservice_up variable and/or the results of your curl command?

It is the results of the curl command. You when webservice is up I get a response that contains "200 OK".

hymie!

unread,
Jun 5, 2018, 12:10:57 PM6/5/18
to
In our last episode, the evil Dr. Lacto had captured our hero,
mike <mikaelp...@hotmail.com>, who said:
> Den tisdag 5 juni 2018 kl. 16:52:47 UTC+2 skrev hymie!:
>> In our last episode, the evil Dr. Lacto had captured our hero,
>> mike <mikaelp...@hotmail.com>, who said:
>>
>> > webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
>> > until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]
>> > do
>> > printf '.'
>> > sleep 6
>> > ((repeat++))
>> > done
>>
>> The first problem I see is that you are only making one test, and then
>> you are checking 40 times to see if that test succeeded. Apparently
>> the test failed.
>
>
> I thought this would test twice:
>
>
> until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]

It does not do anything twice. It does two separate tests, once each.

First, you test [ "$repeat" -eq "$max_repeat" ] (that is, "Does the value
of the $repeat variable equal the value of the $max_repeat variable?") If
(and only if) that test returns "false", then you do the second test
[ $webservice_up ] (that is, "What is the value of the $webservice_up
variable?").

If both tests return "false", then it executes the loop and tries the
tests again.

>> The other question is ... do you know exactly what is in the
>> $webservice_up variable and/or the results of your curl command?
>
> It is the results of the curl command. You when webservice is up I
> get a response that contains "200 OK".

Then for some reason, your curl command is failing. Again, I can't
help you here without the actual URL (and access to said URL).

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net

Ben Bacarisse

unread,
Jun 5, 2018, 12:12:10 PM6/5/18
to
No. x=$(cmd) sets x to be the output from cmd. $x just refer to
whatever that string is.

Solution: I'd define a function and call it as often as needed.

It might be better to use the return status from curl, rather than
getting the headers and grepgging, bu I'm not a curl user so I can't
tell you what the exact magic is.

--
Ben.

hymie!

unread,
Jun 5, 2018, 12:16:31 PM6/5/18
to
(I made a mistake in my original post, so I'm trying to cancel that one
and fix it here.)

In our last episode, the evil Dr. Lacto had captured our hero,
mike <mikaelp...@hotmail.com>, who said:
> Den tisdag 5 juni 2018 kl. 16:52:47 UTC+2 skrev hymie!:
>> In our last episode, the evil Dr. Lacto had captured our hero,
>> mike <mikaelp...@hotmail.com>, who said:
>>
>> > webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
>> > until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]
>> > do
>> > printf '.'
>> > sleep 6
>> > ((repeat++))
>> > done
>>
>> The first problem I see is that you are only making one test, and then
>> you are checking 40 times to see if that test succeeded. Apparently
>> the test failed.
>
>
> I thought this would test twice:
>
>
> until [ "$repeat" -eq "$max_repeat" ] || [ $webservice_up ]

It does not do anything twice. It does two separate tests, once each.

First, you test [ "$repeat" -eq "$max_repeat" ] (that is, "Does the value
of the $repeat variable equal the value of the $max_repeat variable?") If
(and only if) that test returns "false", then you do the second test
[ $webservice_up ] (that is, "Does the value of the $webservice_up variable
have a non-zero length?").

If both tests return "false", then it executes the loop and tries the
tests again.

>> The other question is ... do you know exactly what is in the
>> $webservice_up variable and/or the results of your curl command?
>
> It is the results of the curl command. You when webservice is up I
> get a response that contains "200 OK".

Kenny McCormack

unread,
Jun 5, 2018, 12:43:59 PM6/5/18
to
In article <e93dcab5-208c-49e2...@googlegroups.com>,
mike <mikaelp...@hotmail.com> wrote:
...
>ok so [ $webservice ] will not execute the command:
>
>
>$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
>
>?

No, as many others will no doubt chime in, it only executes it once, and
stores the output for future use.

There are many workarounds for this, including using a function (as
mentioned by another poster), but as I see it, you don't need to run it
more than once anyway. As in the example code I posted.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/InsaneParty

Chris Elvidge

unread,
Jun 5, 2018, 1:04:37 PM6/5/18
to
On 05/06/2018 15:17, mike wrote:

> webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')

Might you not be better testing for an open port 80 on
${SONAR_WEB_SERVICE_URL} with nmap?
IME curl does not return if there's no service.

--

Chris Elvidge, England

Jorgen Grahn

unread,
Jun 5, 2018, 5:05:33 PM6/5/18
to
On Tue, 2018-06-05, mike wrote:
> Hi,
>
> I have the following "bash script" for checking if the webservice for our
> sonarQube is up and running.
...
> max_repeat=40;
> repeat=0;

IME, retrying things like this is almost always a mistake. If you try
once and fail, then it's not "up and running", right?

> webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')

Seems a bit overworked and indirect to me. Try 'curl -fso /dev/null $url'
instead. AKA 'curl --fail --silent --output /dev/null $url'. That
way you tell curl to do the job for you.

You may also want to tell curl to fail if DNS lookup takes too long,
or connecting and getting a HTTP response takes too long. I don't
know those options in detail.

My script would roughly be:

#!/bin/bash
url=something

curl -fso /dev/null $url || {
echo "sonar webservice is not running"
exit 1
}

/Jorgen


--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

mike

unread,
Jun 7, 2018, 1:17:46 AM6/7/18
to
Since it takes some time ( differs from time to time) for the service to be up I need to have some kind of polling mechanism. The above example does not take that into account.

//mike

Jorgen Grahn

unread,
Jun 7, 2018, 3:16:06 AM6/7/18
to
On Thu, 2018-06-07, mike wrote:
> Den tisdag 5 juni 2018 kl. 23:05:33 UTC+2 skrev Jorgen Grahn:
>> On Tue, 2018-06-05, mike wrote:
>> > Hi,
>> >
>> > I have the following "bash script" for checking if the webservice for our
>> > sonarQube is up and running.
>> ...
>> > max_repeat=40;
>> > repeat=0;
>>
>> IME, retrying things like this is almost always a mistake. If you try
>> once and fail, then it's not "up and running", right?
...

> Since it takes some time ( differs from time to time) for the
> service to be up I need to have some kind of polling mechanism.

Ok, but why do you have to deal with that situation? Does the service
restart very often, or do you have scenarios/use cases where you
initiate a service start, need to access it immediately, but have no
way of telling when the start finished?

Ideally a service should start synchronously by taking its resources
before going into daemon mode, you can do e.g.

% sudo systemctl start foo && use_foo

I've been in that situation myself, and know it's often outside your
control (someone else's fault) ... but it's worth pointing out.

Jorgen Grahn

unread,
Jun 7, 2018, 3:18:10 AM6/7/18
to
On Thu, 2018-06-07, mike wrote:
> Den tisdag 5 juni 2018 kl. 23:05:33 UTC+2 skrev Jorgen Grahn:
>> On Tue, 2018-06-05, mike wrote:
>> > Hi,
>> >
>> > I have the following "bash script" for checking if the webservice for our
>> > sonarQube is up and running.
>> ...
>> > max_repeat=40;
>> > repeat=0;
>>
>> IME, retrying things like this is almost always a mistake. If you try
>> once and fail, then it's not "up and running", right?
>>
>> > webservice_up=$(curl --noproxy '*' -s --head --request GET ${SONAR_WEB_SERVICE_URL} | grep '200 OK')
>>
>> Seems a bit overworked and indirect to me. Try 'curl -fso /dev/null $url'
>> instead. AKA 'curl --fail --silent --output /dev/null $url'. That
>> way you tell curl to do the job for you.
>>
>> You may also want to tell curl to fail if DNS lookup takes too long,
>> or connecting and getting a HTTP response takes too long. I don't
>> know those options in detail.
>>
>> My script would roughly be:
>>
>> #!/bin/bash
>> url=something
>>
>> curl -fso /dev/null $url || {
>> echo "sonar webservice is not running"
>> exit 1
>> }
>>
> Since it takes some time ( differs from time to time) for the
> service to be up I need to have some kind of polling mechanism. The
> above example does not take that into account.

That was on purpose -- see above (and separate posting).

But if you need retries, why not use curl's --retry and related
options?
0 new messages