如何从的bash / shell脚本评估HTTP响应codeS? [英] How to evaluate http response codes from bash/shell script?

查看:155
本文介绍了如何从的bash / shell脚本评估HTTP响应codeS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我缺少明显的感觉,但还没有和男子得手[袅袅| wget的] 或谷歌(HTTP让这么糟糕的搜索术语)。我在寻找一个快速和放脏修复我们的网络服务器经常失败的一个,返回状态code 500与错误信息。一旦发生这种情况,就需要重新启动。

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)

提出的解决方案是创建一个运行每5分钟,检查 HTTP cron作业://本地主机:8080 / 。如果这个带有状态code 500返回时,Web服务器将重新启动。该服务器将重新启动在一分钟之内,因此没有需要检查的重新启动已在运行。

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.

问题的服务器是Ubuntu的8.04最小安装安装运行它所需要目前只够包。没有硬性要求做在bash的任务,但我想它在这样一个最小的环境,而无需安装任何更多的跨preters运行。

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.

(我足够熟悉脚本的命令/选项,HTTP状态code分配给一个环境变量就足够了 - 这就是我一直寻找,但没有找到。)

(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.)

推荐答案

我还没有500 code测试这一点,但它适用于其他类似200,302和404。

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)

这篇关于如何从的bash / shell脚本评估HTTP响应codeS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆