V
V
Venot2020-08-25 19:19:09
linux
Venot, 2020-08-25 19:19:09

How to rotate logs depending on the percentage of the occupied space?

There is a directory with logs, how can I delete old files if the folder size is more than a certain size? Does logrotate have such an opportunity or write a bash script and add it to cron?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
Dmitry, 2020-08-25
@q2digger

logrotate has the ability to run scripts before and after rotation. Everything can be implemented in these scripts.

A
Alexey Dmitriev, 2020-08-25
@SignFinder

logrotate doesn't depend on folder size - because there are no folders in linux, there are directories.
But the size of the directory is also not there - there is only the size of the files.
Therefore, you can disable automatic rotation, write a script that will check the size of the directory by cron and give the logrotate command with a specific config.

P
pfg21, 2020-08-25
@pfg21

a task in systemd to exceed the directory size, in the task a script deleting the ten oldest files.
then finish with a rasp in place.

J
Jordan_Belforts, 2020-08-25
@Jordan_Belforts

Oh, it reminds me of a test task in UCOZ (UKIT)

V
Vitaly Karasik, 2020-08-26
@vitaly_il1

Google "logrotate based on directory size".
As far as I understand, this example is quite working:
For instance (/etc/logrotate.d/foo):
/var/log/foo # logs path
{
rotate 4 # limit number of files
size 10M # limit to 10 Mb
weekly # if less than size limit, it will rotate by expired time
compress # compressed file
delaycompress
}
In case logrotate scrip runs every day, it will be implement if file's size is bigger than 10Mb. Max log files at the folder (/var/log/foo) is limited to 4, Otherwise it will remove old files

F
Foxcloud Foxcloud, 2020-08-28
@FoxCloud

Hello!
Log rotation helps to achieve several goals:
- Preservation of logs of a certain period.
- Reducing disk space occupied.
To achieve the result, there are several options.
Recommended option #1.
Increase priority by: having access to logs for exactly the period of time that you need. Lower priority on saving disk space by rotating logs.
Thus, you set up log rotation and, if there is not enough disk space, you upgrade / add disk space. There are options for both dedicated servers and virtual ones. At the moment, the resource of disk space is very cheap and becomes cheaper every year.
Option number 2
Two options that can complement your current logroate settings:
rotate 2
size 500M
These options can help you save disk space by removing some of the logs.
Log rotation will work if the file reaches 500MB and the maximum number of logs is limited. All older ones will be deleted.
In this way, you can strictly limit the amount of space in the directory that your logs can take up.
Not a recommended option, because here the priority is set to reduce the occupied disk, and not to the presence of logs, which may be necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question