11

I need to check the output of apachectl configtest in a bash script and restart if everything looks good without outputting the command to the screen

var =sudo apachectl configtest

If var contains "Syntax OK" then

sudo apachectl graceful

How to do it?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
dev810vm
  • 111
  • 1
  • 1
  • 3
  • I'm not exactly sure what you mean by "outputting to the screen," but if you run the script manually, you can see the output of the commands it runs in the terminal. You can also use `>` to save the command output to a file like so: `[command] > [filepath]` – ifconfig May 06 '17 at 02:08
  • 4
    `apachectl graceful` automatically runs configtest before restarting, according to the [documentation](https://httpd.apache.org/docs/current/programs/apachectl.html). – Steve Trout May 06 '17 at 02:08
  • Does it fail if the configtest finds issues? – dev810vm May 06 '17 at 02:12
  • Right. It does not stop or start the daemon if `configtest` fails. – that other guy May 06 '17 at 02:39
  • 3
    Your question is probably more appropriate for the StackExchange sites [**Super User**](http://superuser.com/), [**Server Fault**](http://serverfault.com/) or [**Unix & Linux**](http://unix.stackexchange.com/). – David C. Rankin May 06 '17 at 04:19
  • @R.Gadeev You shouldn't edit whitespace in code of questions if it changes the behaviour of the code, as it does here – you might accidentally fix the problem (not the case here, though). – Benjamin W. May 06 '17 at 05:11
  • @BenjaminW. OK. I have understood, thanks. – R. Gadeev May 06 '17 at 07:35

3 Answers3

10

I know this is the old thread and the question doesn't suit this particular site. Anyway, looking for the same question, this page was shown as the first search result. So, I am posting here my final solution, for a reference.

configtestResult=$(sudo apachectl configtest 2>&1)

if [ "$configtestResult" != "Syntax OK" ]; then
    echo "apachectl configtest returned the error: $configtestResult";
    exit 1;
else
    sudo apachectl graceful
fi

This thread contains a clue regarding catching configtest output.

Y. E.
  • 687
  • 1
  • 10
  • 29
3

As @slm says on the link, you can used -q for quiet. That way it don't output the command on the screen. Make sure there no space between the variable, the '=' and the command as @William Pursell says here. After that test if your variable contains "Syntax OK". The following code snippet does that.

var1=$(sudo apachectl configtest)

if echo $var1 | grep -q "Syntax OK"; then
    sudo apachectl graceful
fi
Community
  • 1
  • 1
berrytchaks
  • 839
  • 10
  • 18
2

The bash syntax you are after in your first command is probably "command substitution":

VAR=$(sudo apachectl configtest)

VAR will contain the output of the commandline.

But, if you just want to know if the output contains "Syntax OK", do it like this:

sudo apachectl configtest | grep -q "Syntax OK" && proceed || handle-error

where proceed and handle-error are your functions that handle your ok and error cases, respectively.

(Note the -q option of grep to hide the output of the apachectl command.)

Ville Oikarinen
  • 400
  • 1
  • 11
  • This works in spirit, but there's some issues. Without redirecting apachectl's output (`apachectl configtest 2>&1`), `proceed` will never happen. Also, If `proceed` exits with an error, `handle-error` will also run, which you may not necessarily want. – Dale C. Anderson May 02 '19 at 19:27