我正在使用bash为Docker容器编写运行状况检查脚本。应该检查URL返回状态200及其内容。
有没有一种方法可以使用wget来检查URL返回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
}
答案 0 :(得分:1)
#!/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