One gotcha you need to be aware of, (on *NIX at least), if your application/daemon still has the logfile open for writing your rename in line 28/31 will not remove that association. Instead, the application will continue writing to the logfile.1 and will do so until you eventually remove it, so you might end up losing data between the tar and the rm (consider using the Perl builtin unlink here instead BTW). Also, your application might get confused if the file it is writing to (and for which it holds a valid open file descriptor) suddenly vanishes underneath it.

One method of dealing with this is to send a HUP signal to the application (provided you know which process it is and can figure out the process id) after renaming the file. Most long-running daemons take that as a signal to reopen log files and will do the right thing. You could also figure out which processes have an open file descriptor on the log file (by using the *NIX utility lsof for example) and then send a SIGHUP to that process. Or use one of the modules andreas1234567 pointed out, they have methods for dealing with this exact problem.

Lastly, a non-perl method: take a look at logrotate, it's a common utility used for rotating logs with all bells and whistles built in.


All dogma is stupid.

In reply to Re: Log Rotation by tirwhan
in thread Log Rotation by Danikar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.