inspirednz
inspirednz

Reputation: 5087

Bash script to test status of site

I have a script for testing the status of a site, to then run a command if it is offline. However, I've since realised because the site is proxied through Cloudflare, it always shows the 200 status, even if the site is offline. So I need to come up with another approach. I tried testing the site using curl and HEAD. Both get wrong response (from Cloudflare).

What I have found is that HTTPie command gets the response I need. Although only when I use the -h option (I have no idea why that makes a difference, since visually the output looks identical to when I don't use -h).

Assuming this is an okay way to go about reaching my aim ... I'd like to know how I can test if a certain string appears more than 0 times.

The string is location: https:/// (with three forward slashes).

The command I use to get the header info from the actual site (and not simply from what Cloudflare is dishing up) is, http -h https://example.com/.

I am able to test for the string using, http -h https://example.com | grep -c 'location: https:///'. This will output 1 when the string exists.

What I now want to do is run a command if the output is 1. But this is where I need help. My bash skills are minimal, and I am going about it the wrong way. What I came up with (which doesn't work) is:

#!/bin/bash
STR=$(http -h https://example.com/)

if (( $(grep -c 'location: https:///' $STR) != 1 )); then
        echo "Site is UP"
        exit
else
        echo "Site is DOWN"
        
        sudo wo clean --all && sudo wo stack reload --all
fi

Please explain to me why it's not working, and how to do this correctly.

Thank you.

ADDITIONS:

What the script is testing for is an odd situation in which the site suddenly starts redirecting to, literally, https:///. This obviously causes the site to be down. Safari, for instance, takes this as a redirection to localhost. Chrome simply spits the dummy with a redirect error, ERR_INVALID_REDIRECT.

When this is occurring, the headers from the site are:

HTTP/2 301
server: nginx
date: Thu, 12 May 2022 10:19:58 GMT
content-type: text/html; charset=UTF-8
content-length: 0
location: https:///
x-redirect-by: WordPress
x-powered-by: WordOps
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer, strict-origin-when-cross-origin
x-download-options: noopen
x-srcache-fetch-status: HIT
x-srcache-store-status: BYPASS

I choose to test for the string location: https:/// since that's the most specific (and unique) to this issue. Could also test for HTTP/2 301.

The intention of the script is to remedy the problem when it occurs, as a temporary solution whilst I figure out what's causing Wordpress to generate such an odd redirect. Also in case it happens whilst I am not at work, or sleeping. :-) I will have a cron job running the script every 5 mins, so at least the site is never down for longer than that.

Upvotes: 0

Views: 1330

Answers (1)

tripleee
tripleee

Reputation: 189958

grep reads a file, not a string. Also, you need to quote strings, especially if they might contain whitespace or shell metacharacters.

More tantentially, grep -q is the usual way to check if a string exists at least once. Perhaps see also Why is testing “$?” to see if a command succeeded or not, an anti-pattern?

I can see no reason to save the string in a variable which you only examine once; though if you want to (for debugging reasons etc) probably avoid upper case variables. See also Correct Bash and shell script variable capitalization

The parts which should happen unconditionally should be outside the condition, rather than repeated in both branches.

Nothing here is Bash-specific, so I changed the shebang to use sh instead, which is more portable and sometimes faster. Perhaps see also Difference between sh and bash

#!/bin/sh

if http -h https://example.com/ | grep -q 'location: https:///' 
then
    echo "Site is UP"
else
    echo "Site is DOWN"        
fi

sudo wo clean --all && sudo wo stack reload --all

For basic diagnostics, probably try http://shellcheck.net/ before asking for human assistance.

Upvotes: 1

Related Questions