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?

7
  • 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] Commented May 6, 2017 at 2:08
  • 4
    apachectl graceful automatically runs configtest before restarting, according to the documentation. Commented May 6, 2017 at 2:08
  • Does it fail if the configtest finds issues? Commented May 6, 2017 at 2:12
  • Right. It does not stop or start the daemon if configtest fails. Commented May 6, 2017 at 2:39
  • 3
    Your question is probably more appropriate for the StackExchange sites Super User, Server Fault or Unix & Linux. Commented May 6, 2017 at 4:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

apachectl configtest writes to stderr. var1 captures nothing.
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.)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.