Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if HTTP returns HTTP/1.1 200 OK using wget

Tags:

linux

shell

here i am trying to check if wget -s returns HTTP/1.1 200 OK using shell script. i am using this wget command to get the http status from url

#!/bin/sh
#
URL="http://www.example.com"
wget -S $URL

if i get it returns HTTP/1.1 200 OK, then it should exit else run scr.sh script.

How can i do this?

like image 909
CJAY Avatar asked Oct 25 '25 21:10

CJAY


2 Answers

Here I'm using exit code of silent wget command:

wget -q --spider $URL 
if [ $? -ne 0 ] ; then
  echo "do something"
fi

In case if response is an error it runs some command.

like image 96
Yuriy Gatilin Avatar answered Oct 27 '25 18:10

Yuriy Gatilin


Using wget:

SHOULD_EXIT=$(wget --server-response --content-on-error=off ${URL} | awk -F':' '$1 ~ / Status$/ { print $2 ~ /200 OK/ }')

${SHOULD_EXIT} will be 1 for 200 OK and 0 otherwise.

like image 44
Johnsyweb Avatar answered Oct 27 '25 18:10

Johnsyweb