Home > Net >  Bash script to test status of site
Bash script to test status of site

Time:05-18

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 redirect: 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"
        sudo wo clean --all && sudo wo stack reload --all
        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.

CodePudding user response:

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.

CodePudding user response:

Suggesting:

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

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