1

Apache's configtest is useful to catch syntax errors. On success it prints "Syntax OK" and when running apachectl configtest in a bash script I want to suppress this.

I have tried the usual output redirections to /dev/null, assigning the results to a variable like so:

AOUTPUT=$(/usr/sbin/apache2ctl -t && /usr/sbin/apache2ctl graceful)

but "Syntax OK" is always output.

Can this be suppressed?

2
  • 1
    It is probably writing to stderr, so try apache2ctl -t 2>/dev/null
    – meuh
    Nov 24, 2020 at 10:14
  • @meuh thanks, that works. It also suppresses the message when there is a syntax error. Can you see a way to get the text into a variable, so I can match against it and choose what to output?
    – PeterB
    Nov 24, 2020 at 14:20

1 Answer 1

3

Inside $() you can still redirect stderr to be the same as stdout, and then it will be captured by the assignment. For example:

if AOUTPUT=$(/usr/sbin/apache2ctl -t 2>&1)
then /usr/sbin/apache2ctl graceful
else rc=$?; printf "%s\n" "$AOUTPUT" >&2; exit $rc
fi

I added the >&2 so the errors still come out on stderr, and the exit to try to duplicate the original error code from the command, but you would only really need them if you were putting this in a shell script and wanted to preserve those features.

1
  • Thank you, that does exactly what I need. I am using this in a shell script, and it's useful to have cron email me the errors and not the "Syntax OK" response
    – PeterB
    Nov 25, 2020 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .