How to create a loop in bash that is waiting for a web server response?

How to create a loop in bash that is waiting for a web server response?

He should type "." every 10 seconds or so, and wait for the server to start responding.

Update, this code checks if I got a good response from the server.

 if curl --output / dev / null --silent --head --fail "$ url";  then
   echo "URL exists: $ url"
 else
   echo "URL does not exist: $ url"
 fi
+48
bash
Aug 10 2018-12-12T00:
source share
6 answers

Combining the question with the chepner answer, this worked for me:

until $(curl --output /dev/null --silent --head --fail http://myhost:myport); do printf '.' sleep 5 done 
+77
Jan 17 '14 at 15:22
source share

httping is good for this. simple, clean, quiet.

 while ! httping -qc1 http://myhost:myport ; do sleep 1 ; done 

bye / to etc. is a personal prefix.

+4
Apr 03 '15 at 17:08
source share

An interesting puzzle. If you do not have access or asynchronous access to your client, you can try grepping tcp sockets as follows:

 until grep '***IPV4 ADDRESS OF SERVER IN REVERSE HEX***' /proc/net/tcp do printf '.' sleep 1 done 

But this is a busy wait with an interval of 1 second. You probably need more permission. It is also global. If another connection is added to this server, your results are invalid.

0
Aug 10 '12 at 15:59
source share

The use of backticks is deprecated . Use $( ) instead:

 until $(curl --output /dev/null --silent --head --fail http://myhost:myport); do printf '.' sleep 5 done 
0
Apr 18 '14 at 15:35
source share

wait-on is a cross-platform command line utility and Node.js API that will wait for files, ports, sockets, and http (s) resources to appear: https://github.com/jeffbski/wait-on

For example, wait: 8080 in 5 seconds and do NEXT_CMD if this happens.

 wait-on -t 5000 http-get://localhost:8080/foo && NEXT_CMD 
0
Sep 13 '17 at 8:35
source share

if you need to check if the server is accessible, cause a restart or something else, you can try to make wget on the server and analyze the answer or error, if you get 200 or even 404, the server you can change the wget timeout with - timeout = seconds, you can set the timeout to 10 seconds and loop until the grep result of the response reaches the result.

I don't know if this is really what you are looking for, or if you really need bash code.

-one
Aug 10 2018-12-12T00:
source share



All Articles