Setting log rotation in Centos servers:
Logrotation helps to reduce disk size by rotating log files and compressing them. It helps to prevent over sizing of log files and disk space issue by removing old logs data, creating small chunk of specific numbers of log files.
Default log rotate file is located in /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
# keep 4 weeks worth of backlogs
# create new (empty) log files after rotating old ones
# use date as a suffix of the rotated file
# uncomment this if you want your log files compressed
# RPM packages drop log rotation information into this directory
# no packages own wtmp and btmp -- we'll rotate them here
create 0664 root utmp
create 0600 root utmp
Linux comes with defualt file settings for many modules which can be found inside /etc/logrotate.d/ . For example if we want to do log rotation for mysql we can find default mysql file called mysqld, so full path is : /etc/logrotate.d/mysqld.
Do the vi to edit the file and uncomment settings:
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !
# Then, un-comment the following lines to enable rotation of mysql's log file. default name for mysql log is mysqld.log so the setting contains log rotation settings for mysqld.log file. Do check your my.cnf file of mysql settings mostly located in /etc/my.cnf and make sure log path is /var/log/mysqld.log. We can add multiple log files like error log , query log e.t.c in same settings. These settings can also be copied to my.cnf file or can be added as seperate file in /etc/logrotate.d/ folder.
create 640 mysql mysql
# just if mysqld is really running
if test -x /usr/bin/mysqladmin && \
/usr/bin/mysqladmin ping &>/dev/null
create: mode owner group: Create log file with given mode and user/group settings.
notifempty : Do not rotate the log if it is empty
weekly : Log files are rotated if the current weekday is less then the weekday of the last rotation or if more then a week has passed since the last rotation
rotate 3 : Log files are rotated 3 times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated
missingok : If the log file is missing, go on to the next one without issuing an error message.
compress : Old versions of log files are compressed with gzip to save disk space.
postrotate: start of script.
endscript: The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside a log file definition. In our case we are checking is mysql is running and flushing logs.
More setting options can be found from http://linuxcommand.org/man_pages/logrotate8.html