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?

  • 7
    Worth having a look at logrotate. linuxconfig.org/setting-up-logrotate-on-redhat-linux – steve Dec 16 '16 at 17:08
  • Logrotate is an option which we have for to rotate log files once it reaches to given size in logrotate.conf file .but we need to restrict the file once it reaches to 10MB and we no need anymore backups,new lines for that file . – Hari Kn Dec 19 '16 at 17:51

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

Would an emlog module help? I see it mentioned at https://serverfault.com/questions/133320/turning-a-log-file-into-a-sort-of-circular-buffer , but I'm not sure whether it's a good fit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.