views:

983

answers:

3

I have the feeling that I'm missing the obvious, but have not succeeded with man [curl|wget] or google ("http" makes such a bad search term). I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returning status code 500 with an error message. Once this happens, it needs to be restarted.

As the root cause seems to be hard to find, we're aiming for a quick fix, hoping that it will be enough to bridge the time until we can really fix it (the service doesn't need high availability)

The proposed solution is to create a cron job that runs every 5 minutes, checking http://localhost:8080/. If this returns with status code 500, the webserver will be restarted. The server will restart in under a minute, so there's no need to check for restarts already running.

The server in question is a ubuntu 8.04 minimal installation with just enough packages installed to run what it currently needs. There is no hard requirement to do the task in bash, but I'd like it to run in such a minimal environment without installing any more interpreters.

(I'm sufficiently familiar with scripting that the command/options to assign the http status code to an environment variable would be enough - this is what I've looked for and could not find.)

+2  A: 

I haven't tested this on a 500 code, but it works on others like 200, 302 and 404.

response=$(curl --write-out %{http_code} --silent --output /dev/null servername)
Dennis Williamson
Nice - thanks: I've already found --write-out, but missed the --output /dev/null. When all the content comes with it, the response code gets lost in too much information, so I simply did not see it...
Olaf
+1  A: 

With netcat and awk you can handle the server response manually:

if netcat 127.0.0.1 8080 <<EOF | awk 'NR==1{if ($2 == "500") exit 0; exit 1;}'; then
GET / HTTP/1.1
Host: www.example.com

EOF

    apache2ctl restart;
fi
marco
Nice - thanks a lot
Olaf
+1  A: 

Here:

url='http://localhost:8080/'
status=$(r=(IFS=' ';$(curl -Is --connect-timeout 5 "${url}" || echo 1 500));echo ${r[1]})
[ status -eq 500 ] && bounce # assuming the bounce script is called 'bounce'

Or put it all on one line:

[ 500 -eq $(r=(IFS=' ';$(curl -Is --connect-timeout 5 'http://localhost:8080/' || echo 1 500));echo ${r[1]}) ] && bounce

To explain, the HTTP response always contains the server status as part of the first line of the response, like:

HTTP/1.1 200 OK
HTTP/1.0 404 NOT FOUND

The script just uses curl to do a HEAD request to localhost:8080. It converts the HTTP header to an array and returns the second element. To simplify some failure handling, if the HEAD fails to connect within 5 seconds or curl fails for some reason, 500 is also returned.

nicerobot
Thank you very much.
Olaf