如何评估http响应代码从bash/shell脚本?

我有一种感觉,我错过了明显的,但没有成功的man [curl|wget]或谷歌(“http”使这样一个糟糕的搜索词)。我正在寻找一个快速和肮脏的修复我们的网络服务器之一,经常失败,返回状态码500与错误消息。一旦发生这种情况,就需要重新启动。

由于根本原因似乎很难找到,我们的目标是快速修复,希望这将足以弥补时间,直到我们真正修复它(服务不需要高可用性)

建议的解决方案是创建一个每5分钟运行一次的cron作业,检查http://localhost:8080/。如果返回状态码为500,则web服务器将重新启动。服务器将在一分钟内重新启动,因此不需要检查已经在运行的重新启动。

所讨论的服务器是ubuntu 8.04最小安装,只安装了足够运行当前所需的包。在bash中执行该任务没有硬性要求,但我希望它能在这样一个最小的环境中运行,而不需要安装任何解释器。

(我非常熟悉脚本,命令/选项将http状态代码分配给一个环境变量就足够了——这是我一直在寻找的,但没有找到。)

321252 次浏览

我还没有在500代码上测试过,但它可以在200、302和404等其他代码上运行。

response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)

注意,——write-out提供的格式应该被引用。 根据@ibai的建议,添加--head来做一个HEAD请求。这将节省检索成功时的时间,因为页面内容不会被传输

使用netcat和awk,您可以手动处理服务器响应:

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

虽然接受响应是一个很好的答案,但它忽略了失败场景。如果请求中有错误或连接失败,curl将返回000

url='http://localhost:8080/'
status=$(curl --head --location --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${url})
[[ $status == 500 ]] || [[ $status == 000 ]] && echo restarting ${url} # do start/restart logic

注意:这稍微超出了请求的500状态检查,以确认curl甚至可以连接到服务器(即返回000)。

从它创建一个函数:

failureCode() {
local url=${1:-http://localhost:8080}
local code=${2:-500}
local status=$(curl --head --location --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${url})
[[ $status == ${code} ]] || [[ $status == 000 ]]
}

测试获得500:

failureCode http://httpbin.org/status/500 && echo need to restart

测试获取错误/连接失败(即000):

failureCode http://localhost:77777 && echo need to start

测试没有得到500:

failureCode http://httpbin.org/status/400 || echo not a failure
curl --write-out "%{http_code}\n" --silent --output /dev/null "$URL"

的工作原理。如果没有,则必须按回车键查看代码本身。

遵循3XX重定向并打印所有请求的响应代码:

HTTP_STATUS="$(curl -IL --silent example.com | grep HTTP )";
echo "${HTTP_STATUS}";

这可以帮助评估HTTP状态

var=`curl -I http://www.example.org 2>/dev/null | head -n 1 | awk -F" " '{print $2}'`
echo http:$var

补充一下@DennisWilliamson的评论:

@VaibhavBajpai:试试这个:response=$(curl——write-out \n%{http_code}——silent——output - servername) -结果中的最后一行将是响应代码

然后,您可以使用类似以下的代码从响应中解析响应代码,其中X可以表示一个regex,以标记响应的结束(此处使用json示例)

X='*\}'
code=$(echo ${response##$X})

参见子字符串移除:http://tldp.org/LDP/abs/html/string-manipulation.html

另一个变化:

       status=$(curl -sS  -I https://www.healthdata.gov/user/login  2> /dev/null | head -n 1 | cut -d' ' -f2)
status_w_desc=$(curl -sS  -I https://www.healthdata.gov/user/login  2> /dev/null | head -n 1 | cut -d' ' -f2-)

这里有一个冗长但容易理解的脚本,灵感来自nicerobot的解决方案,它只请求响应头,并避免使用IFS作为建议的在这里。它在遇到响应>= 400时输出一个反弹消息。这个回显可以用一个反弹脚本代替。

# set the url to probe
url='http://localhost:8080'
# use curl to request headers (return sensitive default on timeout: "timeout 500"). Parse the result into an array (avoid settings IFS, instead use read)
read -ra result <<< $(curl -Is --connect-timeout 5 "${url}" || echo "timeout 500")
# status code is second element of array "result"
status=${result[1]}
# if status code is greater than or equal to 400, then output a bounce message (replace this with any bounce script you like)
[ $status -ge 400  ] && echo "bounce at $url with status $status"

我今天需要快速演示一些东西,于是想到了这个。我想如果有人需要类似OP要求的东西,我可以把它放在这里。

#!/bin/bash


status_code=$(curl --write-out %{http_code} --silent --output /dev/null www.bbc.co.uk/news)


if [[ "$status_code" -ne 200 ]] ; then
echo "Site status changed to $status_code" | mail -s "SITE STATUS CHECKER" "my_email@email.com" -r "STATUS_CHECKER"
else
exit 0
fi

这将在每200个状态变化时发送一封电子邮件提醒,所以它是愚蠢的和潜在的贪婪。为了改进这一点,我将考虑遍历几个状态代码,并根据结果执行不同的操作。

我不喜欢这里的答案,将数据与状态混合。 发现了这个: 添加-f标志使curl失败,并从标准状态变量$?< / p >

https://unix.stackexchange.com/questions/204762/return-code-for-curl-used-in-a-command-substitution < a href = " https://unix.stackexchange.com/questions/204762/return-code-for-curl-used-in-a-command-substitution " > < / >

我不知道它是否适合这里的每个场景,但它似乎符合我的需求,我认为它更容易使用

下面是我的实现,它比以前的一些答案更详细一些

curl https://somewhere.com/somepath   \
--silent \
--insecure \
--request POST \
--header "your-curl-may-want-a-header" \
--data @my.input.file \
--output site.output \
--write-out %{http_code} \
> http.response.code 2> error.messages
errorLevel=$?
httpResponse=$(cat http.response.code)




jq --raw-output 'keys | @csv' site.output | sed 's/"//g' > return.keys
hasErrors=`grep --quiet --invert errors return.keys;echo $?`


if [[ $errorLevel -gt 0 ]] || [[ $hasErrors -gt 0 ]] || [[ "$httpResponse" != "200" ]]; then
echo -e "Error POSTing https://somewhere.com/somepath with input my.input (errorLevel $errorLevel, http response code $httpResponse)" >> error.messages
send_exit_message # external function to send error.messages to whoever.
fi
  1. 假设您已经为应用程序实现了一个停止和启动脚本。创建一个如下所示的脚本,检查应用程序url的http状态,并在出现502时重新启动:

httpStatusCode=$(curl -s -o /dev/null -w "%{http_code}"https:// {your_url} /) if [$httpStatusCode = 502];然后执行sh /{path_to_folder}/stopscript.sh sh /{path_to_folder}/startscript.sh fi

.sh
  1. 实现一个cron作业,每5分钟调用一次这个脚本。假设上面的脚本名为checkBootAndRestart.sh。那么你的crontab应该看起来像- */5 * * * * /{path_to_folder}/checkBootAndRestart.sh