Bash – limiting size of logfile from a variable

bashfileslimitlogssize;

How can I limit the size of a log file, which is saved in the form of 'foo.txt', from within a bash script please? I want to put in the variables 'LOGFILE=50 mb' and it uses that size, or whatever size LOGFILE is.

This is on Debian 7, fully up-to-date.

Best Answer

You need to use logrotate. Do something like this

cat /etc/logrotate.conf

/path/foo.txt {

    size 50M

    create 700 root root

    rotate 5

}

size 50M – logrotate runs only if the filesize is equal to (or greater than) this size.

create – rotate the original file and create the new file with specified permission, user and group.

rotate – limits the number of log file rotation. So, this would keep only the recent 5 rotated log files.