Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'd like to use curl to send a HTTP HEAD request to a URL to verify that it is working correctly (server is up and sends a successful response). At the end, I need the final HTTP response code returned and URL (after following any redirects). Finally, I need to set a hard timeout (a period of time after which the call will fail, regardless of where in the request curl is).

Does curl support all this? What options do I need to use to accomplish this?

share|improve this question

1 Answer 1

curl -I will generate a HEAD request.

Adding the -L option will make it follow redirects.

Timeouts can be controlled with several options depending on which bit of the request you want to time out.

From the man page:

--connect-timeout <seconds>
          Maximum time in seconds that you allow the connection to the server to take.  This only limits the connection phase, once curl has connected this option is of no more use. See also the -m/--max-time option.

    -m/--max-time <seconds>
          Maximum time in seconds that you allow the whole operation to take.  This is useful for preventing your batch jobs from hanging for hours due to slow networks or links going down.  See also the --connect-timeout option.

Another couple of timeout-related options also worth looking at are:

--retry
--retry-delay
--retry-max-time
share|improve this answer
    
Thanks, what about retrieving the final URL and response code? –  jrdioko Dec 12 '11 at 23:06
1  
The final URL will be the value of the Location: header of the previous response. The final response code is returned normally. Chaining all of these together with grep would look a bit like this: curl -s -I -L -m 4 http://example.com | grep -E "^Location|^HTTP" | tail -2. The -s option stops curl printing out the download progress. tail -2 restricts the output to just the final Location header and the final HTTP response code. –  Ladadadada Dec 12 '11 at 23:38

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.