3

I have a server with about 300 virtual hosts. When I want to make sure a specific httpd.conf file is loaded into the Virual Host config and the syntax is correct, I run apachectl -S. The problem is, though, I get a ton of output.

I've tried apacectl -S | grep 'foo' and apachectl -S > foo.txt to try and make this data a little bit more manageable, but the output of the command is not conducive to grepping or shoving into a text file.

When I try apachectl -S | grep 'foo', it simply returns the entire output of apachectl -S.

When I try apachectl -S > foo.txt, foo.txt is an empty file.

This may have something to do with how the server is configured, because I am able to successfully grep on my local machine.

Any suggestions?

CamelBlues
  • 303
  • 4
  • 10
  • What is the prolem you have with the grep output. Show us what you get and tell us why it's not what you want. – user9517 Mar 23 '12 at 15:27
  • Updated. I think this has something to do with how the shell on the server is configured... possibly it's terminating line endings differently? Does that make sense? – CamelBlues Mar 23 '12 at 16:08
  • what shell are you using, which OS ? – user9517 Mar 23 '12 at 16:10
  • 1
    I'm running bash on CentOS 5.5, so maybe that's not it :( – CamelBlues Mar 23 '12 at 16:16
  • What you pipe to grep is stdout, are you sure that apachectl -S is dumping to stdout, or stderr? Redirect stderr to stdout, grep on that. – Marcin Mar 23 '12 at 16:38

2 Answers2

6

The output of apachectl is sent to stderr. The commands you are using attempt to filter stdout. To use grep in the way you are describing, redirect stderr to stdout, like so:

apachectl -S 2>&1 | grep 'blah'
Insyte
  • 9,394
  • 3
  • 28
  • 45
3

Haha, looks like this question was already answered on Stack Overflow:

https://stackoverflow.com/questions/6505932/how-to-filter-apache-virtual-host-listing-using-grep

Because apachectl -S (which is really a wrapper for httpd -S) sends its output through STDERR, I'm going to need to redirect STDERR to STDOUT, then grep it.

apachectl -S 2>&1 | grep 'foo'

Thanks, S.O.!

CamelBlues
  • 303
  • 4
  • 10