Confirmation of site or host service status

Asked

Viewed 175 times

0

I built this script to run on Linux, but it is not working as expected.

I’m using the curl to find the state of the HTTP connection.

He returns to me:

HTTP/1.1 200 OK
Mais alguns conteúdos do site http...

This is the script:

#!/bin/bash
curl-v --head --output /dev/null http://site:port/folder >> relatorios.txt
if ! HTTP_CODE=200 do
echo "operando servico web"; then
echo "Serviço Online"
else
echo "Serviço Offline"
exit 1
fi
exit

I expected it to respond with 200 code online service when it is operating on the network, and offline service with any other code that is termed when a web page is not available, 404, 500 etc..

I turned off the test machine to validate if the message would appear offline but still show online. I would need him to show me the status of when it’s OK and if for any other reason the page goes down, report that the page is out. I would like to use this result field HTTP/1.1 200 OK because it changes when the page is out.

I did this script on the terminal by vi, would like to use the curl to check the status of the web page, but it did not come out as expected. Someone has any suggestions?

2 answers

1

You can use it like this:

#!/bin/bash

HTTP_CODE=$(curl -o relatorios.txt  -L -s -w "%{http_code}" http://www.yoursite.com/pagina.html)

if [ "$HTTP_CODE" == "200" ]; then
        echo "operando servico web"
        echo "Serviço Online"
else
        echo "Serviço Offline"
        exit 1
fi
  • Validated the action applied, thank you :-)

  • 1

    blz. Mark as useful answer (arrow above), Please! :-)

0

An example bro! can not format the text on this site sorry! more should help your problem!

status=$(Curl -s --head google.com | awk '$2 ː "200" ')

if [ -n "$status" ]; then

echo "operating web service"

echo "Online Service"

Else

echo "Offline Service";Exit 1

fi

Exit

  • presented running problem in line 3 option"-n" the message /directory not found/

  • Yo bro that code msm worked Aki!!! more anyway I saw that already found the solution! / I apologize if I could not help!!

  • Okay, thanks for your help. VLW man

Browser other questions tagged

You are not signed in. Login or sign up in order to post.