How to create a loop in bash that is waiting for a webserver to respond?

前端 未结 6 1125
半阙折子戏
半阙折子戏 2020-12-12 16:48

How to create a loop in bash that is waiting for a webserver to respond?

It should print a \".\" every 10 seconds or so, and wait until the server starts to respond.

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 17:21

    I wanted to limit the maximum number of attempts. Based on Thomas's accepted answer I made this:

    attempt_counter=0
    max_attempts=5
    
    until $(curl --output /dev/null --silent --head --fail http://myhost:myport); do
        if [ ${attempt_counter} -eq ${max_attempts} ];then
          echo "Max attempts reached"
          exit 1
        fi
    
        printf '.'
        attempt_counter=$(($attempt_counter+1))
        sleep 5
    done
    

提交回复
热议问题