Limit size for a particular logfile in Linux

limitlogsrhelsize;

There are a few log files which occupy a huge amount of space and I want to restrict it to 10MB in Linux.

For example: test.log file should not exceed 10MB; logs should stop going to that file.

Can you please share the possible steps or commands to accomplish this?

Best Answer

Inspired by alex's answer to a similar question, if you can control the location of the log file such that it can be placed in a separate directory, then you could create a loop-mount backed by a file of a given size. Writes to that subdirectory will then be limited by the size of the backing file. This would start to get messy if you needed to handle multiple log files.

## adjust the size to taste for filesystem overhead
dd if=/dev/zero of=./10mb.img bs=1M count=12 
/sbin/mkfs.ext4 10mb.img
mkdir logdir
sudo mount -t ext4 -o loop 10mb.img logdir

sudo dd if=/dev/urandom of=logdir/test.log
dd: writing to ‘logdir/test.log’: No space left on device
21033+0 records in
21032+0 records out
10768384 bytes (11 MB) copied, 2.3511 s, 4.6 MB/s
Related Question