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.)
works. If not, you have to hit return to view the code itself.
With netcat and awk you can handle the server response manually:
I needed to demo something quickly today and came up with this. Thought I would place it here if someone needed something similar to the OP's request.
This will send an email alert on every state change from 200, so it's dumb and potentially greedy. To improve this, I would look at looping through several status codes and performing different actions dependant on the result.
I haven't tested this on a 500 code, but it works on others like 200, 302 and 404.
As suggested by @ibai, add
--head
to make a HEAD only request. This will save time when the retrieval is successful since the page contents won't be transmitted.Another variation:
To follow 3XX redirects and print response codes for all requests: