|
|
#1 |
|
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 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 Code:
% cat somefile /var/log/httpd /volumes/data/anotherfolder /volumes/data/yetanother 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. |
|
|
|
|
|
#2 |
|
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" ... |
|
|
|
|
|
#3 |
|
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 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. |
|
|
|
|
|
#4 | |||||||||||||||||||
|
League Commissioner
Join Date: Jan 2002
Posts: 5,536
|
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. |
|||||||||||||||||||
|
|
|
|
|
#5 |
|
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 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. |
|
|
|
|
|
#6 |
|
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. |
|
|
|
|
|
#7 |
|
All Star
Join Date: Jan 2002
Posts: 579
|
LOL! too funny
::tears of joy:: I feel the mighty nerdy power! i got that ![]() Vonleigh |
|
|
|
![]() |
|
|