Link to home
Start Free TrialLog in
Avatar of deepakjena_2003
deepakjena_2003

asked on

Apache Log Rotation

Hi,

I am facing lots of problem due to heavy logs generated by my Webserver.

I am using the following shell script to rotate the logs...but it's not working properly.
==========================
#!/bin/bash
HTTPD_LOGS_DIR=/scratch/content/logs/apache       # path to httpd log dir
YYMMDD=`/bin/date +%Y%m%d`

# This section will move Access Logs #

cd $HTTPD_LOGS_DIR/access
pwd
for i in `find . -name "*.$YYMMDD" -atime +0`
do
 gzip $i
 mv *.gz $HTTPD_LOGS_DIR/logs_backup/.
done
==================================


Can any body suggest me a good Shell script to rotate the Apache logs...


Waiting for a quick Response....

Thanks
Deepak
Avatar of hernst42
hernst42
Flag of Germany image

Hi deepakjena_2003,
You can use the rotatelogs(2) command that comnes with apache(2) to write to the logfile. After logrotet has begun a new file you can zip the old one. If you change a log-file manual you need to reload the webserver so he closes the old file-pointer and starts with a new one. That might be your problem with your script.

Cheers!
Avatar of deepakjena_2003
deepakjena_2003

ASKER

Hi,

i have around 20 domains configured in a single box.

So separate logs are generate for separate Domains.

Log files are generated in different fashion as i have mentioned in the scripts...

So based on this i need a script to automate the proces....

Thanks
Deepak
If you reload the webserver after you have archived the files your script should work. Some infomation might be lost during the time you started archiving and the reload of the webserver
Problems i am facing from my existing scripts.....

1)  Log files are created automatically once i delete the log files after i archive these logs.(without   restarting apache)
2)  sometimes getting incorrect timestamp for the log files.
3)  i tried "> logfile" option also without deleting the log files, but i didn't workout.

Etc......

Thanks
Deepak
1)  Log files are not created automatically once i delete the log files after i archive these logs.(without   restarting apache)
SOLUTION
Avatar of cyb3rj0hn
cyb3rj0hn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The last guy (cyb3rj0hn) has the correct answer:  you should be using logrotate
Hi John,

here i am putting the access log line from my httpd.conf file.....
------------------------------------------------------------------------
CustomLog "| /apps/apache/2.0.48/bin/rotatelogs /data/access/abc.com_access.%Y%m%d 86400" common
------------------------------------------------------------------------

above line create access log for abc.com in the format "abc.com_access.20041217"

so daily one log file is created for this domain.

Requirement :
i want to compress and move all logs to different folder(ex: /data/log_backup ) except current date logs and also delete all
logs from "/data/log_backup" folder which are 7 days old.


I guess this will not work out for me, so i have to remove "rotatelogs" implementation from httpd.conf file and follow your technique.

i have some doubts....

1) is "logrotate" calles to "/etc/logrotate.conf" file ?
2) shall i write a script file which will implement "logrotate" and call that script file in crontab ?

Still in Doubt...

Please give me the steps to implement "logrotate" and "logrotate.conf" for this...

Thanks
Deepak



ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with hernst42.
Thanks for all your help....