mandar joshi
mandar joshi

Reputation: 25

Linux script with curl to check recursively if web service is up

I wanted to create a Linux script to check recursively if web service is up and if it is down then wait for sometime and check again.

I have written below script using curl which will check whether web service is up and if yes it will echo message and same with fail.

test_command='curl -sL \
    -w "%{http_code}\\n" \
    "http://www.google.com:8080/" \
    -o /dev/null \
    --connect-timeout 3 \
    --max-time 5'

if [ $(test_command) == "200" ] ; 
then
   echo "OK" ;
else
   echo "KO" ;
fi

I am trying now to add recursive code in else part so that if web service is not yet up after deployment then it will add some wait in else part and will check the status again after some seconds for that web service.

Upvotes: 0

Views: 479

Answers (1)

U880D
U880D

Reputation: 12142

In respect to the comment about recursive functions in Bash, I've created a small test and which seems to work at a first glance.

./conTest.sh

#! /bin/bash

  test_command()
  {

  http_code=$(curl -sL -x "http://localhost:3128" -w "%{http_code}\\n" "https://www.google.com/" -o /dev/null --connect-timeout 3 --max-time 5)

  if [ ${http_code} == "200" ] ;
  then
    echo "OK" ;
  else
    echo "KO" ;
    sleep 1 ; 
    test_command ;
  fi

  }

  test_command ;

Maybe one can provide a more elegant solution or has an other idea.

Upvotes: 1

Related Questions