LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-03-2012, 05:41 AM   #1
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Rep: Reputation: 5
logrotate


Thought it would be straight forward. But life is not at all easy as you may think.

Here is a simple log.conf

Code:
/my/path/to/logfiles/* {
  maxage 10
  rotate 0
}
And I expected all the files older than 10 days be deleted.

If I do

Code:
logrotate -d log.conf
It comes up with messages 'log does not need rotating' for files even couple of months older.

If I do

Code:
logrotate -f log.conf
It deletes all the files even if they are few seconds old.

Have I misunderstood the logrotate or is the conf wrong or am I missing something?

Here is the configuration:
CentOS 5
logrotate 3.7.4-9

Thanks
 
Old 07-03-2012, 05:51 AM   #2
hasan_sust
LQ Newbie
 
Registered: May 2007
Posts: 23

Rep: Reputation: 0
Just Follow this


/my/path/to/logfiles/* {
daily
rotate 10
compress
copytruncate
notifempty
missingok
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}


Here log will rotate daily and take backup of last 10 days log. log will be saved as .gz format
 
Old 07-03-2012, 06:09 AM   #3
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Original Poster
Rep: Reputation: 5
Thanks for your reply hasan_sust.

But all I want is,

* run the log rotate everyday (which is obviously done by the cron.daily)
* check if the file is older than 10 days
* throw it away if so

what is wrong with my config?

Cheers

Last edited by guna_pmk; 07-03-2012 at 06:22 AM.
 
Old 07-04-2012, 12:19 AM   #4
hasan_sust
LQ Newbie
 
Registered: May 2007
Posts: 23

Rep: Reputation: 0
rotate 10
It will keep files of last 10 days and delete older files.

in your configuration you mention rotate 0>> that's why It deletes all the files even if they are few seconds old.

Hope you understand this point.

to rotate log manually, you have to give the following command

logrotate -f log.conf


For you fiirst query>> run the log rotate everyday (which is obviously done by the cron.daily)

logrotate is run as a daily cron job. in /etc/cron.daily there is a file name logrotate. that's why it executes daily
 
Old 07-04-2012, 06:27 AM   #5
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Original Poster
Rep: Reputation: 5
Thanks hasan.

Let's say I have the following files in /my/path/to/logfiles:

-rw-r--r-- 1 user user 11K May 16 12:37 file1.log
-rw-r--r-- 1 user user 250 May 17 09:54 file2.log
-rw-r--r-- 1 user user 22K May 14 10:41 file3.log
-rw-r--r-- 1 user user 1.3K Jun 19 13:43 file4.log
-rw-r--r-- 1 user user 1.7K Jun 29 11:14 file5.log
-rw-r--r-- 1 user user 36K Jun 25 12:35 file6.log
-rw-r--r-- 1 user user 37K Jun 29 14:18 file7.log

and with the original config, I expect files file1,2 and 3 to be deleted.

From the man page, for rotate, I understand that it will keep given number of copies of a file. For example, in the above list, if file1 is more than 10 days older, it will be copied to file1.log.1(for example) and after another 10 days it will be moved to file1.log.2 upto file1.log.10 and then deleted after that.I don't think it is something like what you have said (that's what the maxage is for). rotate will simply keep the given number of copies either compressed or not depending upon the compress/nocompress parameter. A rotate 0 will just delete the file when a condition is met (like daily, monthly, maxage, size etc).

Additionally the -f option is to force the logrotation even if logrotate thinks that a file is not ready for a rotation (f is not for file but force).

Please let me know if I have misunderstood.

Cheers
 
Old 07-04-2012, 10:12 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
maxage will delete rotated logs only, not the original files. And with rotate 0 you avoid creating log files.

So, the question is: what has to be older than ten days? The original files, or the rotated ones (being compressed or not). Do you get so many files just in ten days?
 
Old 07-04-2012, 11:25 AM   #7
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Original Poster
Rep: Reputation: 5
Hi Reuti,

Thanks.

I think that's what I have misunderstood. You are right. maxage removes the rotated files of count days. I have misunderstood it to the maxage of the actual files.

To answer your question, I want the actual file to be removed (without rotating) if it is older than 10 days. I think I have given a use case in #5.

10 days is just an example case. Practically it will be between 30 to 90 days on different situations.

Please let me know if you need more information in this regard.

Cheers
 
Old 07-04-2012, 12:12 PM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by guna_pmk View Post
To answer your question, I want the actual file to be removed (without rotating) if it is older than 10 days. I think I have given a use case in #5.
But stuff is added to the logfiles by the syslog several times a day. Do you want the files to be removed if nothing was added for ten days, and so modification date wasn’t updated? And if you do so, all old information therein would be gone.
 
Old 07-05-2012, 02:49 AM   #9
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Original Poster
Rep: Reputation: 5
Yeah, that's fine. I do not care whatever that is in the file. I do not want them anyhow after the x number of days.

PS: To be honest those are the log files created by a set of automated scripts developed by me. They provide some forensic information about any failures and will not be needed after few days.
 
Old 07-05-2012, 05:39 AM   #10
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
This is no task for logrotate then IMO. A plain cron job where you look for the modification date and delete the files would do already. Create a file in /etc/cron.daily:
Code:
#!/bin/sh
find /var/log -name "*.log" -daystart -mtime +10 -exec rm {} \;
 
1 members found this post helpful.
Old 07-05-2012, 05:55 AM   #11
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

I was some time ago setting the logrotate for a custom log file from this example.

It's quite straightforward.

My log file is daily rotated (keeping previous day compressed):
Code:
/var/www/example.com/cgi-bin/tracker/logfile.txt {
    daily
    missingok
    rotate 1
    notifempty
    copytruncate
    compress
}
by that "rotate 1" keeps only one archived log and current file.
 
Old 07-06-2012, 02:57 AM   #12
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

Original Poster
Rep: Reputation: 5
Thanks Guys,

Reuti has got a point there. In fact, that is how I have implemented it currently.

The reason I wanted to move towards logrotate is because it is a standard way of 'handling' the log files. Whether it is the right way or not should the logrotate not be working for this task?

Anyhow, I am not going to spend much time on this. I going to stay with the current setup.
 
Old 07-06-2012, 01:14 PM   #13
Ghostwheel
Member
 
Registered: Jun 2004
Location: Mid Michigan
Distribution: CentOS, Ubuntu
Posts: 41

Rep: Reputation: 0
Quote:
#!/bin/sh
find /var/log -name "*.log" -daystart -mtime +10 -exec cat /dev/null > {} \;
This would actually be the better way to do it as you won't end up with errors from just deleting the files. It will just truncate them.

Last edited by Ghostwheel; 07-06-2012 at 01:15 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Logrotate help Linux_Kidd Linux - Software 7 03-08-2012 12:34 PM
[SOLVED] logrotate or logrotate.conf; problems with sftp slufoot80 Linux - Server 1 11-05-2011 07:50 AM
Why we use logrotate ? shipon_97 Linux - Newbie 4 01-09-2008 09:15 AM
logrotate sidra Red Hat 10 08-01-2006 01:05 AM
Logrotate Help! MaverickApollo Linux - Software 3 02-19-2004 12:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration