Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm working with a system where I would like to use the dateext function of logrotate (or some other way) to add the date to a logfile when it is rotated. However in this system it is important that no logging is missing and dateext will overwrite any existing files (which will happen if logrotate is called twice on a day).

Is there a reliable way to prevent dateext to overwrite existing files, but instead make another file?. It is acceptable that either no rotate happens or a file is created with a less predictable name (date with an extra number, or the time or something).

share|improve this question
add comment

2 Answers

dateext supports the %s format specifier - the Unix epoch time in seconds.

If you set the dateformat as .%Y-%m-%d.%s, it will look like this:

logfilename.2012-12-11.13572638495

This changes every second, so the chances of two identical files are very remote.

share|improve this answer
add comment

You could use prerotate/endscript to make sure that there is no file with the name you are about to use, but if you are concerned with rotations happening at any odd time this could be tricky. The worst case is you have two rotates of the same file running at the same time (2nd started before 1st completed), and if both determine that there is no name conflict, then one will overwrite another.

An easier approach could be changing the rotated file name to include more exact date, e.g. including nanoseconds in the timestamp. This should make it very unlikely for names to conflict. If you want to be sure that there's no name conflict use mktemp, but then you will get "ugly" names. Logic of such name change should go to prerotate|postrotate/endscript section of logrotate config.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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