Table of Contents

Log Rotation - Setting up log rotation on a server

Author: Michelle McCausland

Introduction

Log rotation is setup as a bash script that manages the log files on the server to ensure older logs are deleted and that logs are zipped up to save space.

The log rotation script, is found in ~/script/log_rotation.sh on a server that has this configured already.


Log rotation script

The log rotation script is a bash script that performs a set of commands on a given interval through the crontab.

In the case of this example the log rotation script will remove any logs older than 28 days. It will also zip up the logs to allow for more storage of logs:

log_rotation.sh

#!/bin/bash 

/usr/bin/find /home/scotty/logs/grails/ -type f -mtime +28 -exec rm {} \;

/usr/bin/find /home/scotty/logs/grails/ -type f -mtime +1 -exec gzip *.log.20* {} \;

Important to note the following “-mtime +1”. It is essential that this is included when zipping up the logs so the current day's logs to not get zipped up too. This will cause a watchdog!


Set Up Log Rotation

chmod 774 log_rotation.sh

To learn more about linux file permissions I recommend reading the following: http://linuxcommand.org/lc3_lts0090.php

Type sudo crontab -e to display the crontab:

The line we are concerned with for log_rotation is:

#Log Rotation Job - Setup 31st Jan 2017 - SUPPORT-330

40 0 * * * /home/scotty/script/log_rotation.sh > /home/scotty/script/log_rotate.out 2>&1

This should be included in the crontab to ensure the log_rotation.sh script is executed.

To learn more about how the crontab works I would recommend reading the following: http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/