簡體   English   中英

檢查狀態代碼和內容的 wget 響應

[英]Check wget response for both status code and content

我正在使用 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
}
#!/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM