Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 08-03-2002, 09:05 PM   #1
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
more on rotating apache logs

Hello,

Well it's time to extend my apache rotate script for other folders (as I have more than one virtual server).

What I'd like to know, is if there is a way to use a local file to tell it where these logs are kept.

So basically I have:

Code:
#!/bin/sh

# Extra monthly tasks (1)

# 1. Rotate Apache log files

echo -n "Rotating Apache log files:"
cd /var/log/httpd/

for i in access_log error_log; do
whole script snipped...
done
And what I want is:

Code:
#!/bin/sh

# Extra monthly tasks (1)

# 1. Rotate Apache log files

echo -n "Rotating Apache log files:"

for X in somefile(list of dirs); do

cd X
echo -n "Rotating X's logs"

	for i in access_log error_log; do
	whole script snipped...
	done
done
And somefile would look like this:

Code:
% cat somefile
/var/log/httpd
/volumes/data/anotherfolder
/volumes/data/yetanother
Or is there a better way? Maybe it'd be smarter to keep the list of dirs within the file. What do you think?

Also, where would I keep the somefile? (I don't know much about where unix admins put stuff traditionally).



Thanks,
Vonleigh

Last edited by vonleigh; 08-03-2002 at 09:10 PM.
vonleigh is offline   Reply With Quote
Old 08-03-2002, 11:02 PM   #2
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
your somefile is a fine approach to this issue. you want to separate the target from the code to generalize the code and so you edit your config file with changes and you could keep it in /usr/local/admin to guarantee global availability and that it remains around.

as for how to use your config file, one way is to load a variable with the contents of the somefile...
Code:
...
echo -n "Rotating Apache log files:"

foo=`cat /usr/local/admin/apacheLogs.conf`

for X in $foo; do

# or just  for X in `cat /pathto/filename`

cd X
echo -n "Rotating X's logs"
...
let us know if that works.
mervTormel is offline   Reply With Quote
Old 08-04-2002, 01:00 AM   #3
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
Hello,

Thanks MT for your great help, I'm happy you're with us on the forums. The script works beautifully, I would just like to ask one more thing to really polish it off.

In the apacheLogs.conf file there is only addresses, I would like to have some kind of explanation at the beginning of the file. Would I have to grep this out of the 'cat' or something, or will the 'if' know to ignore lines beginning with #?

For reference, I'm posting the complete script and output, just in case anyone needs it.

Output:
Code:
% sudo sh /etc/monthly.local
Rotating Apache log files:
Rotating /var/log/httpd's logs
 access_log/usr/sbin/apachectl graceful: httpd gracefully restarted
 error_log/usr/sbin/apachectl graceful: httpd gracefully restarted
Rotating /Volumes/data/user/website/logs's logs
 access_log/usr/sbin/apachectl graceful: httpd gracefully restarted
 error_log/usr/sbin/apachectl graceful: httpd gracefully restarted
Script:
Code:
% cat /etc/monthly.local 
#!/bin/sh

# Extra monthly tasks (1)

# 1. Rotate Apache log files

echo "Rotating Apache log files:"

# apacheLogs.conf contains dirs for logs that need rotating
logs=`egrep -v '#|^$' /usr/local/admin/apacheLogs.conf`

for x in $logs; do
cd $x
echo "Rotating ${x}'s logs"

for i in access_log error_log; do
    if [ -f "${i}" ]; then
        echo -n " $i"
        if [ -x /usr/bin/gzip ]; then gzext=".gz"; else gzext=""; fi
        if [ -f "${i}.6${gzext}" ]; then mv -f "${i}.6${gzext}" "${i}.7${gzext}"; fi
        if [ -f "${i}.5${gzext}" ]; then mv -f "${i}.5${gzext}" "${i}.6${gzext}"; fi
        if [ -f "${i}.4${gzext}" ]; then mv -f "${i}.4${gzext}" "${i}.5${gzext}"; fi
        if [ -f "${i}.3${gzext}" ]; then mv -f "${i}.3${gzext}" "${i}.4${gzext}"; fi
        if [ -f "${i}.2${gzext}" ]; then mv -f "${i}.2${gzext}" "${i}.3${gzext}"; fi
        if [ -f "${i}.1${gzext}" ]; then mv -f "${i}.1${gzext}" "${i}.2${gzext}"; fi
        if [ -f "${i}.0${gzext}" ]; then mv -f "${i}.0${gzext}" "${i}.1${gzext}"; fi
        if [ -f "${i}" ]; then 
                mv -f "${i}" "${i}.0"; 
                apachectl graceful; 
                sleep 600;   
                if [ -x /usr/bin/gzip ]; then 
                        gzip -9 "${i}.0"; 
                fi; 
        fi
        touch "${i}" && chmod 644 "${i}"
    fi
done
done


Vonleigh

Last edited by vonleigh; 08-04-2002 at 01:32 AM.
vonleigh is offline   Reply With Quote
Old 08-04-2002, 01:14 AM   #4
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
Quote:
...Would I have to grep this out of the 'cat' or something, or will the 'if' know to ignore lines beginning with #?...

exactly. instead of the cat, use egrep -v like so...

logs=`egrep -v '#|^$' /usr/local/admin/apacheLogs.conf`

that will take care of comments (#) and blank lines (^$) so you will get the canonical list you need.

thanks for generalizing a log squeaker.
mervTormel is offline   Reply With Quote
Old 08-04-2002, 01:30 AM   #5
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
Perfect!

I'll go and edit the previous message to add that change for posterity. Thanks again MT.


Code:
% cat /usr/local/admin/apacheLogs.conf 
# List of all locations where apache logs
# are present, so they can be rotated monthly.
# see monthly.local

/var/log/httpd
/Volumes/data/user/website/logs
As I side note you made me remember a tip from UGU that basically said not to 'cat grep' but instead use grep directly, good coding mt.

As another note I've been wanting to learn about using Regular Expressions for a while (as they are used alot and seem pretty powerful), I found this site:

<http://etext.lib.virginia.edu/helpsheets/regex.html>


Vonleigh

Last edited by vonleigh; 08-04-2002 at 01:36 AM.
vonleigh is offline   Reply With Quote
Old 08-04-2002, 02:10 AM   #6
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
yeah, someone once said "if your using cat with one argument, you're probably doing something wrong."

--
"awk! i guess i oughta get a grep on regexp's too," he sed.
mervTormel is offline   Reply With Quote
Old 08-04-2002, 03:30 AM   #7
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
LOL! too funny

::tears of joy:: I feel the mighty nerdy power! i got that


Vonleigh
vonleigh is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:49 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.