Log rotation in bash

This is a little bash script that rotate logs as when the file reach a specified limit.

#!/bin/bash

# FILE TO MONITOR
FILE="/log/network_monitor.log"

touch ${FILE}
MaxFileSize=4096

while true
do
     sh network_monitor.sh >> ${FILE}
# SIZE IN BYTES** 
    file_size=`du -b ${FILE} | tr -s '\t' ' ' | cut -d' ' -f1`
    if [ ${file_size} -gt ${MaxFileSize} ];then   
        timestamp=`date +%s`
        mv ${FILE} ${FILE}.$timestamp
        touch ${FILE}
    else
    	exit
    fi
done

Leave a Reply

Your email address will not be published. Required fields are marked *