LoginSignup
6
5

More than 5 years have passed since last update.

wgetを使って200 OKが返るかを判定

Last updated at Posted at 2015-10-29

wgetのスパイダーモードを使って、その結果に200 OKが含まれるかをgrep -cで判定。

result=`wget --spider -nv [URL] 2>&1 | grep -c '200 OK'`
if [ ${result} -ne 1 ]; then
  echo "[URL] is down."
  exit 1
fi

別のやり方としては$?でgrepの返り値をとるパターン。

wget --spider -nv [URL] 2>&1 | grep '200 OK'
result=$?
if [ ${result} -ne 0 ]; then
  echo "[URL] is down."
  exit 1
fi

参考サイト

6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5