Welcome! Log In Create A New Profile

Advanced

how to limit access.log file size?

Posted by the_guv 
how to limit access.log file size?
October 14, 2009 12:13PM
How to do this, keeping the last messages for, say, the last month, else have a maximum byte size.

This is handy, for example, if you have a VPS or dedicated with limited storage but a bunch of sites, and so lots of access logs.

I guess the same procedure could be carried out for error.log? Less of an issue/less recommendable, sure.

Many tx.

***************************************************************************************************
http://guvnr.com .. now publishing the ultimate 40-part VPS/Nginx copy/paste guide ..

Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible .. (featuring Nginx) ..
http://www.guvnr.com/web/web-dev/setup-unmanaged-vps-newbies-v-p-s-bible/
Re: how to limit access.log file size?
October 15, 2009 08:32AM
The easiest method is to use logrotate(8).

Alternatively you can write your own shell script to do it. Something like:

[code]
#!/bin/bash

# set nginx pid to a variable
pid=`cat /path/to/nginx.pid`

cd /path/to/nginx-logs

# remove the oldest log file and rename others
rm -f *7.gz
rename .6.gz .7.gz *.6.gz
rename .5.gz .6.gz *.5.gz
rename .4.gz .5.gz *.4.gz
rename .3.gz .4.gz *.3.gz
rename .2.gz .3.gz *.2.gz
rename .1 .2 *.1
rename .log .log.1 *.log

# reopen the log files with a USR1 signal
kill -USR1 $pid

gzip *2

exit
[/code]

Set this to run via cron daily. This will save seven days worth of logs (you can do it with any number you like), and gzip the logs from 2-7 days ago. The most recent "old" logs will be left unzipped so it can be looked at if desired. This assumes that all logs are in the same directory. If they are not, then you would need to adjust accordingly. Logs will be in format of

[list]
[*] access.log
[*] access.log.1
[*] access.log.2.gz
[*] access.log.3.gz
[*] access.log.4.gz
[*] access.log.5.gz
[*] access.log.6.gz
[*] access.log.7.gz
[/list]

YMMV of course.

--
Jim Ohlstein
Re: how to limit access.log file size?
October 15, 2009 08:45AM
Hi again Jim .. darn kind .. appreciate that.

Cracking!

Will try both methods. Anything worth reporting, I'll post it here.

***************************************************************************************************
http://guvnr.com .. now publishing the ultimate 40-part VPS/Nginx copy/paste guide ..

Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible .. (featuring Nginx) ..
http://www.guvnr.com/web/web-dev/setup-unmanaged-vps-newbies-v-p-s-bible/
Re: how to limit access.log file size?
October 15, 2009 07:28PM
ok .. for logrotate, here's what i figured ..

/home/public_html/*/*/log/*.log /usr/local/nginx/logs/*.log {
weekly
missingok
rotate 7
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /usr/local/nginx/logs/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}

.. anything stick out badly there?

***************************************************************************************************
http://guvnr.com .. now publishing the ultimate 40-part VPS/Nginx copy/paste guide ..

Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible .. (featuring Nginx) ..
http://www.guvnr.com/web/web-dev/setup-unmanaged-vps-newbies-v-p-s-bible/
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 300
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready