Shell script to parse apache httpd line

Hello,

I have serveral SUN Web servers that need to write a KSH to figure out where Apache is installed and, tar up the directory (for backup purposes) once a month. The trick is that Apache is not installed in same location. So when I do "ps -ef| grep httpd" it shows on some boxes from /usr/local and, on some shows /app. the ones from /app I have the following
ps -fe|grep httpd | awk '{print $8}' to get where apache is running from like so:
www 26228 26215 0 16:01:02 ? 0:00 /app/apache-idmadmin/bin/httpd -k

but, on some other boxes I have to modify my ps to ps -ef| grep httpd | awk '{print $9}' because of the following
www 10303 10295 0 Feb 10 ? 1:28 /usr/local/apache2/bin/httpd -k start
. I would like to write a script that works no matter where httpd is running from.

Appreciate any help

Try this :

ps -ef | grep [h]ttpd | sed 's:^[^/]*::;s: .*::'

or

ps -ef | sed '/[h]ttpd/!d;s:^[^/]*::;s: .*::'

Note :
[h]ttpd i instead of just httpd is a tip to avoid the " grep -v grep "
You also can speed up the process by displaying the first occurrence you will find and then quit the sed loop (by adding a ;q at the end of the sed command), so that if you have multiple httpd deamon, it will only display the first occurrence found.

ps -ef | grep [h]ttpd | sed 's:^[^/]*::;s: .*::;q'

If you have more than 1 apache so several httpd running at different location you might use the command without the ;q and add a | uniq

Even better, you can even save the grep pipe with :

ps -ef | sed '/[h]ttpd/!d;s:^[^/]*::;s: .*::;q'
1 Like

Thanks a lot ctsgnb. This is awesome. exactly what I needed. I had sort and, uniq already for my first akw '{print $8}' . your help greatly appreciated