Home > IT Architecture, Linux, Systems > using logrotate to rotates log files(per site log,by day,reserve last 5 days)

using logrotate to rotates log files(per site log,by day,reserve last 5 days)

November 8th, 2010

The needs as follows:
1.rotate the apache and nginx log files(access log,error log),by day and compress the log file;
2.keep last 5 days' log files,the others throw away automatically;
3.using the date as the suffix of the log file.
The implementation as follows:
In apache:
#/bin/bash
#by doxer.org
echo -E '/usr/local/httpd/logs/*.log {
daily
rotate 5
compress
notifempty
dateext
lastaction
/usr/local/httpd/bin/apachectl -k graceful
endscript
}' > /etc/logrotate.d/httpd_log
/etc/init.d/crond restart

In nginx:

#/bin/bash
echo -E '/usr/local/nginx/logs/*.log {
daily
rotate 5
compress
notifempty
dateext
lastaction
/usr/local/nginx/sbin/nginx -s reload
endscript
}' > /etc/logrotate.d/nginx_log
/etc/init.d/crond restart

PS:logrotate most commonly used options
logrotate -d /etc/logrotate.conf #to debug
logrotate -f /etc/logrotate.conf #to debug,force running logrotate by now.

The you may think what's the mechanism of logrotate?Maybe it has something related to crond!We can see in /etc/cron.daily has the following line:(in file /etc/cron.daily/logrotate)
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

In debian:
#!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

This proves the assumption is right.logrotate does implement it's functionality by crond.

Good Luck!


  1. November 8th, 2010 at 09:33 | #1

    Giant Bicycle engages in designing, manufacturing, and selling giant bicycles. It offers bikes and parts and accessories, such as apparel, gloves, socks, caps, helmets, framesets, road and mountain framesets, indoor cycling trainers. There’s a good reason that people are excited about giant bikes.

Comments are closed.