检查状态代码和内容的 wget 响应
Check wget response for both status code and content
我正在使用 bash 为 docker 容器编写健康检查脚本。它应该检查 URL 返回状态 200 及其内容。
有没有一种方法可以使用 wget 来检查 URL returns 200 并且 URL 的内容包含某个词(在我的例子中这个词是 "DB_OK") 而最好只调用一次?
StatusString=$(wget -qO- localhost:1337/status)
#does not work
function available_check(){
if [[ $StatusString != *"HTTP/1.1 200"* ]];
then AvailableCheck=1
echo "availability check failed"
fi
}
#checks that statusString contains "DB_OK", works
function db_check(){
if [[ $StatusString != *"DB_OK"* ]];
then DBCheck=1
echo "db check failed"
fi
}
#!/bin/bash
# -q removed, 2>&1 added to get header and content
StatusString=$(wget -O- 'localhost:1337/status' 2>&1)
function available_check{
if [[ $StatusString == *"HTTP request sent, awaiting response... 200 OK"* ]]; then
echo "availability check okay"
else
echo "availability check failed"
return 1
fi
}
function db_check{
if [[ $StatusString == *"DB_OK"* ]]; then
echo "content check okay"
else
echo "content check failed"
return 1
fi
}
if available_check && db_check; then
echo "everything okay"
else
echo "something failed"
fi
输出:
availability check okay
content check okay
everything okay
我正在使用 bash 为 docker 容器编写健康检查脚本。它应该检查 URL 返回状态 200 及其内容。
有没有一种方法可以使用 wget 来检查 URL returns 200 并且 URL 的内容包含某个词(在我的例子中这个词是 "DB_OK") 而最好只调用一次?
StatusString=$(wget -qO- localhost:1337/status)
#does not work
function available_check(){
if [[ $StatusString != *"HTTP/1.1 200"* ]];
then AvailableCheck=1
echo "availability check failed"
fi
}
#checks that statusString contains "DB_OK", works
function db_check(){
if [[ $StatusString != *"DB_OK"* ]];
then DBCheck=1
echo "db check failed"
fi
}
#!/bin/bash
# -q removed, 2>&1 added to get header and content
StatusString=$(wget -O- 'localhost:1337/status' 2>&1)
function available_check{
if [[ $StatusString == *"HTTP request sent, awaiting response... 200 OK"* ]]; then
echo "availability check okay"
else
echo "availability check failed"
return 1
fi
}
function db_check{
if [[ $StatusString == *"DB_OK"* ]]; then
echo "content check okay"
else
echo "content check failed"
return 1
fi
}
if available_check && db_check; then
echo "everything okay"
else
echo "something failed"
fi
输出:
availability check okay
content check okay
everything okay