0

I want to check is my website online or not using BASH script. Currently i am trying to check if curl response error then restart APACHE but its not working because i am using cloud flare. I am not a BASH script developer. I need help to make it possible to check JSON response from my URL if its true then OKAY otherwise restart Apache.

URL="http://apental-calc.com/online.php"

for i in {1..5};do

 curl $URL >/dev/null 2>&1

 if [[ $? -ne 0 ]];then
    echo "Restarting Apache2"
    service apache2 restart
    sleep 5
    continue

 else
    echo "Working"
    exit 0
 fi

done

codeforester
  • 39,467
  • 16
  • 112
  • 140
goluwes
  • 13
  • 4
  • Check how exactly? – tripleee Oct 15 '18 at 09:27
  • 2
    Also, notice the antipattern -- anything which looks like `cmd; if [[ $? -ne 0 ]]; then` should usually be rewritten as `if ! cmd; then` -- the entire *purpose* of `if` is to run a command and check its exit status. – tripleee Oct 15 '18 at 09:28
  • i am not bash developer i don't know what you trying to say.. i want to check if ststaus is okay then do nothing otherwise restart apache – goluwes Oct 15 '18 at 09:38
  • Which part of the JSON do you want to examine, how do you know if it's okay or not? – tripleee Oct 15 '18 at 09:38
  • Re: the antipattern, see e.g. William Pursell's answer here: https://stackoverflow.com/questions/7101995/what-does-if-eq-0-mean-for-shell-scripts – tripleee Oct 15 '18 at 09:45

1 Answers1

1

I guess what you are trying to recover is a HTTP Code stating that the request was successful, in that case you can try this:

HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $URL)

This will print the HTTP status of the request done, for instance:

HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://www.google.com)
echo $HTTP_RESPONSE
301

200 and 30X codes indicate a working server. If you don´t get result, then it is time to check CURL exit code:

if [ $? -eq 7 ] then ...

Being 7 the code returned when CURL can not connect to the host:

  1. Failed to connect to host. curl managed to get an IP address to the machine and it tried to setup a TCP connection to the host but failed. This can be because you have specified the wrong port number, entered the wrong host name, the wrong protocol or perhaps because there is a firewall or another network equipment in between that blocks the traffic from getting through

See all codes: https://ec.haxx.se/usingcurl-returns.html

UPDATE: As discussed in the comments, the question is not clear on whether the check is to be done in the HTTP Status code or in a JSON payload (which is missing). In case you want to check the payload, this can be used:

URL Serving a dummy JSON: https://jsonplaceholder.typicode.com/todos/2

{
  "userId": 1,
  "id": 2,
  "title": "quis ut nam facilis et officia qui",
  "completed": false
}

To parse this, this can be used (let´s pretend we want to check the "title"):

$ curl  -s https://jsonplaceholder.typicode.com/todos/2 | grep -Po '"title": ".*' | cut -f 4 -d '"'

This matches only the specified pattern and extracts the value.

Victor
  • 2,450
  • 2
  • 23
  • 54
  • It's not like it really matters how exactly curl fails; if it fails, the test should fail. But the title says something about JSON and you are not examining any JSON at all here. – tripleee Oct 15 '18 at 10:09
  • I guess that by JSON, it is meant the response and its fields (one of them being the HTTP STATUS code). If you want to check some kind of JSON payload, then the response payload should be examined. In that case, an example of "valid" JSON should be provided. – Victor Oct 15 '18 at 10:15
  • Absolutely; the OP should clarify their question. The HTTP result code definitely is not part of any JSON response. – tripleee Oct 15 '18 at 10:17
  • Added a sample on how to check a JSON payload. – Victor Oct 15 '18 at 10:34
  • Okay i got status okay. Help me to compare this. Means if status is not okay. or blank or something else restart apache otherwise exit – goluwes Oct 16 '18 at 09:34