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.)
Here comes the long-winded – yet easy to understand – script, inspired by the solution of nicerobot, that only requests the response headers and avoids using IFS as suggested here. It outputs a bounce message when it encounters a response >= 400. This echo can be replaced with a bounce-script.
this can help to evaluate http status
Here:
Or put it all on one line:
To explain, the HTTP response always contains the server status as part of the first line of the response, like:
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.
To add to @DennisWilliamson comment above:
You can then parse the response code from the response using something like the following, where X can signify a regex to mark the end of the response (using a json example here)
See Substring Removal: http://tldp.org/LDP/abs/html/string-manipulation.html