log rotation in linux using logrotate tool
While maintaining systems disk space in Unix system where huge servers are running and application’s are logging activities continuously, it becomes a burden to check for the disk space usage actively to make sure all is fine.
Very few applications deployed in Java App servers have log rotation in place as usually no body bothers about the log file sizes. but If you are a system administrator then its a big problem as sometimes the server may also go down because of some application logging too much and finally left with 0MB free disk space issue.
There is a good tool logrotate which helps in rotating the logs regularly based on Size/Time of log file. By this method you don’t need to restart the application to rotate the log & re-enable logging to the rotated log file. This saves your disk space and Time too.
Below are the settings that can be used to make a log rotation when a log file reached 500MB in size or becomes 15days old.
You can create a file like applogs_time and applogs_size and follow below steps to rotate the logs using contab and logrotate tool.
Content of applogs_time:
#Rotate *.log files in log directory when a log file becomes 15days old
/opt/server/application/logs/*.log { daily rotate 15 missingok ifempty copytruncate compress delaycompress nomail noolddir }
Content of applogs_size:
#Rotate *.log files in log directory when a log file becomes 500MB
/opt/server/application/logs/*.log { size=500M rotate 15 missingok ifempty copytruncate compress delaycompress nomail noolddir }
To delete very old rotated log files you can put one more entry in crontab to clean up archived log periodically(30days):
find /opt/server/application/logs/ -type f -name "*.log" -mtime +30 -exec rm -f {} \;
Once we have created above two files we need to schedule a cronjob in crontab to run it timely and rotate the logs
Below crontab entries will work: (crontab -e)
*/30 * * * * /usr/sbin/logrotate -s /opt/rotate/time.status /opt/rotate/applogs_size */40 * * * * /usr/sbin/logrotate -s /opt/rotate/size.status /opt/rotate/applogs_time 10 * * * * find /opt/server/application/logs/ -type f -name "*.log" -mtime +30 -exec rm -f {} \;
If you have done above then all logs will rotated time to time and will get cleaned up if not in use for above 30days.
Note: logrotate corrupts the first line of the rotated and active log. So few commands like sed, grep, awk will treat them as binary and may throw errors while executing. To avoid these kind of errors we can delete the first line of the rotated log before doing any kind of activities on the rotated logs.
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.