-2

I have an application which will write to a particular log file as long as the user session is running. What i am looking for is to put a max cap on the size of a log file so it does not grow beyond a particular size. Two scenarios which will be useful are:

  1. Any utility which keeps an eye on the log file and as soon as it reaches the max size start truncating the file content from the start so the application can keep appending the content at the end.

  2. Any utility which supports when creating the file I can specify the max size of that file and when the file reaches that maxsize it should simply not grow beyond that point.

What I don't want is...

...to set up a cron job or a script which will monitor the file size after a particular interval of time (say 1 hour) and then delete its contents at that time.

John K. N.
  • 2,055
  • 1
  • 17
  • 28
  • 5
    Why don't you just use logrotate? It is probably already installed. – Michael Hampton Dec 18 '17 at 06:10
  • 1
    Logrotate perfoms checks for the file size every hour , i want something that will take action immediately if file size grows beyond allocated size.I can set up a cron job to run every minute but that will be putting unnecessary burden on the CPU. So is there any unix utility where i can specify the filename and maxsize and it will keep a constant watch on it and truncate or delete it at that very time when it reaches the specified size. – Anuj Shrivastava Dec 18 '17 at 06:30

3 Answers3

1

As a crude workaround you can mount file as a loop device and write to it. It will be fixed size.

Something like that:

dd if=/dev/zero of=/mnt/image bs=1M count=1024
mkfs.xfs /mnt/image 
mkdir /mnt/1
mount /mnt/image /mnt/1

then log to some /mnt/1/logfile.txt and it will grow only to 1gb.

as i've stated - this is crude workaround

Martynas Saint
  • 1,221
  • 7
  • 15
0

Look at logwatch. This should do what you need, possibly with a bit of scripting. A more advanced option would be something like Splunk or graylog.

Simon Greenwood
  • 1,363
  • 9
  • 12
0

multilog from daemontools will do what you're asking for.

womble
  • 96,255
  • 29
  • 175
  • 230