Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Can logrotate apply multiple rotate rules?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Can logrotate apply multiple rotate rules?

#1 Post by PsySc0rpi0n »

Hello.

Here I am again.
Usually I come here after trying to search in my preferred search engine. But most of the times, either if it's me that can't search properly by providing better search words and/or patterns or because there isn't really much about my search, fact is that I can't find answers to another question of mine.

I have a pretty big debug.log file I need to control how much space it uses. At this moment it's using over 500MB on my drive.
So, I search about how to deal with this and I came across logrotate.

My goal is to rotate the debug.log file in a regular daily basis OR based on size it is using in the drive. So I have 2 conditions to trigger the rotation. Is this possible directly with logrotate or do I need to write small bash script to take care of one of the tasks?


User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#3 Post by PsySc0rpi0n »

You see the answer to my question there? Please tell me where...

pendrachken
Posts: 1394
Joined: 2007-03-04 21:10
Location: U.S.A. - WI.

Re: Can logrotate apply multiple rotate rules?

#4 Post by pendrachken »

Read the man page. It's in there.
fortune -o
Your love life will be... interesting.
:twisted: How did it know?

The U.S. uses the metric system too, we have tenths, hundredths and thousandths of inches :-P

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#5 Post by PsySc0rpi0n »

pendrachken wrote:Read the man page. It's in there.
I already did.
I can't fin any line saying "logrotate can apply multiple rules to a single file" or anything like that!

How would you configure logrotate to perform a rotation based on 1 of 2 rules? And how you say which of the rules to apply first? Or even if you try (by chance) to add 2 rules to see if it works the way you intend to, how do you know to which of the rules the rotation is applied?

None of this questions are explicitly answered there. Otherwise, quote it here!

The man pages don't reply questions like these ever. They explain general usage. Much more can be done "outside" the man pages!

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Can logrotate apply multiple rotate rules?

#6 Post by L_V »

You have a logrotate service normally managed by systemd

Code: Select all

systemd-analyze blame | grep logrotate

systemctl status logrotate.service

systemctl cat logrotate.service

User avatar
fred44nl
Posts: 215
Joined: 2018-07-14 08:54
Has thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#7 Post by fred44nl »

PsySc0rpi0n wrote: I can't fin any line saying "logrotate can apply multiple rules to a single file" or anything like that!
I believe you can't, as you would create conflicting actions.

https://serverfault.com/questions/38810 ... -logrotate

so you would have to study the man pages and look for examples on the net.

https://www.thegeekstuff.com/2010/07/lo ... -examples/
https://linuxconfig.org/logrotate
https://www.tecmint.com/install-logrota ... -in-linux/
Debian 10 - Buster on Compulab Fitlet2
Debian 12 - Bookworm on Acer Spin 1

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Can logrotate apply multiple rotate rules?

#8 Post by Deb-fan »

Think you somewhat answered your own question or headed in a good direction. Something like a script + anacron or cron to set whatever schedule you want. Don't have a ready made script or command. Yep will have to take up the specifics with Google and hands on experimentation. Good luck.
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#9 Post by PsySc0rpi0n »

fred44nl wrote:
PsySc0rpi0n wrote: I can't fin any line saying "logrotate can apply multiple rules to a single file" or anything like that!
I believe you can't, as you would create conflicting actions.

https://serverfault.com/questions/38810 ... -logrotate

so you would have to study the man pages and look for examples on the net.

https://www.thegeekstuff.com/2010/07/lo ... -examples/
https://linuxconfig.org/logrotate
https://www.tecmint.com/install-logrota ... -in-linux/
I came across the first link from serverfault before coming here. But that's not the case.
Second link, the same. I read it. Not the same I need though. The geekstuff one
Third link is new to me but also don't have what I need.. The linuxconfig one.
An I also read the last link. Not what I need either.

I appreciate the effort, I really do.
When I come here I have already did some research.

The multiple rules I need were never mixed on any of those links. I mean like to rotate based on "daily" rule OR "size" rule. Whichever is met first! Meaning that I want a lof file to change name or daily or if it reaches 50MB before a day is gone.

kreemoweet
Posts: 54
Joined: 2013-07-23 08:23

Re: Can logrotate apply multiple rotate rules?

#10 Post by kreemoweet »

Logrotate will not perform real-time monitoring of a log file's size, but you can run logrotate as often as needed as a cron job.
The "daily" rotation parameter for the file can be included in the usual logrotate.conf, and the "size 50M" parameter can be included
in a special logrotate conf file, specified in the cron logrotate command, that applies only to the debug log.

CwF
Global Moderator
Global Moderator
Posts: 2638
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 192 times

Re: Can logrotate apply multiple rotate rules?

#11 Post by CwF »

L_V wrote:You have a logrotate service normally managed by systemd
Follow this lead. I don't have a command list ready for you..
but no need for more than 1 rule per step.
1 Limit log size, start new file at limit
2. set to compress those past logs
3. limit the number of compressed logs

Oh ya, Issue #1 = What is spamming your logs?

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#12 Post by PsySc0rpi0n »

kreemoweet wrote:Logrotate will not perform real-time monitoring of a log file's size, but you can run logrotate as often as needed as a cron job.
The "daily" rotation parameter for the file can be included in the usual logrotate.conf, and the "size 50M" parameter can be included
in a special logrotate conf file, specified in the cron logrotate command, that applies only to the debug log.

I'm going to start researching about this option.

Thanks for the tip!

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#13 Post by PsySc0rpi0n »

CwF wrote:
L_V wrote:You have a logrotate service normally managed by systemd
Follow this lead. I don't have a command list ready for you..
but no need for more than 1 rule per step.
1 Limit log size, start new file at limit
2. set to compress those past logs
3. limit the number of compressed logs

Oh ya, Issue #1 = What is spamming your logs?
Ok...

I'm not sure if "normally managed by systemd" means controlled by crontab or not.
But yeah going to take that route.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#14 Post by PsySc0rpi0n »

Hi again.

I'm back to this.

For some reason, my cron jobs are not running as supposed for logrotate.

I have a config file in /etc/logrotate.d/ to setup a rotation for a file.

Now, if I want to execute the rules in that config file inside /etc/logrotate.d/ hourly, what should I do? Add a file inside /etc/cron.hourly and put what inside it? Just `logrotate`or maybe /usr/sbin/logrotate` ??

User avatar
sickpig
Posts: 589
Joined: 2019-01-23 10:34

Re: Can logrotate apply multiple rotate rules?

#15 Post by sickpig »

so whats stopping you from using systemd.timer as opposed to cron?

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#16 Post by PsySc0rpi0n »

sickpig wrote:so whats stopping you from using systemd.timer as opposed to cron?
I had this working with cron in that past. I see no need to change it now. I want to keep the thread focused in cron and logrotate if possible. After I get this working, I may try systemd.time or whatever that is.

But in the meantime, I read a bit more and did the follwoing:

Created a file named 'myfile' in:

Code: Select all

/etc/logrotate.d
and set the following configs in it for logrotate:

Code: Select all

/media/wdelements/debug.log {
    rotate 5
    daily
    copytruncate
    size 50M
    dateext
    dateformat -%d%m%Y
    maxage 31
}
Then, I created a bah script inside

Code: Select all

/etc/cron.hourly
With the following content:

Code: Select all

#!/bin/bash

if [ -f /etc/logrotate.d/myfile ]; then
	logrotate --verbose /etc/logrotate.d/myfile
else
	echo "File not found!"
fi
And despite logrotate is set to run daily by default, I think this should do the trick, no?

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Can logrotate apply multiple rotate rules?

#17 Post by PsySc0rpi0n »

I'm reviving this thread of mine.

In the meantime I had to re-install my Debian due to some hardware problems. I bought a new SSD because the old was was with hardware failures.
So, this said, I'm again trying to setup logrotate and cron/anacron. But I'm not sure I have this correctly set.

With anacron installed, cron jobs are kind of over ruled by anacron, if I understand correctly.

So, I added a file in /etc/logrotate.d/my-rotate-rules telling the rules I want for the file in question and I verified that inside /etc/anacrontab there are entreies to run cron jobs daily, weekly and monthly. So, I presume this is all I need.

Am I correct?

Edited:

I didn't remember my last post in this thread. I'll try to replicate it.

Post Reply