如何评估来自 bash/shell 脚本的 http 响应代码? - How to evaluate http response codes from bash/shell script?

问题:

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).我有一种感觉,我错过了明显的东西,但没有成功使用man [curl|wget]或 google(“http”使搜索词变得如此糟糕)。 I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returning status code 500 with an error message.我正在寻找对我们的一个经常失败的网络服务器的快速和肮脏的修复,返回状态代码 500 并显示错误消息。 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/ .建议的解决方案是创建一个每 5 分钟运行一次的 cron 作业,检查http://localhost:8080/ If this returns with status code 500, the webserver will be restarted.如果返回状态码 500,则网络服务器将重新启动。 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.有问题的服务器是 ubuntu 8.04 最小安装,只安装了足够的软件包来运行它当前需要的东西。 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.在 bash 中执行任务没有硬性要求,但我希望它在这样一个最小的环境中运行,而无需安装更多的解释器。

(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.) (我对脚本非常熟悉,将 http 状态代码分配给环境变量的命令/选项就足够了 - 这是我一直在寻找但找不到的东西。)


解决方案:

参考一: https://en.stackoom.com/question/9JbJ
参考二: https://stackoom.com/question/9JbJ
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章