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).

link|improve this question
feedback

1 Answer

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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