sadly this is not an easy problem as you need cooperation from the tool that does the logging

I suppose Un*x in the following. The main ideas probably apply to other OSes too.

If the tool opens, writes log info, closes the logfile each time, then a move-truncate scheme is posible

 % mv $log $log.1 && > $log # mv-truncate

If the tool opens just once but does "straight" appends and fluhes, a copy-truncate scheme is usually ok. There is a "logging window that you loose"

 % cp $log $log.1 && > $log # copy-truncate

the rest which is tough :(

  • A bad case but common enough: daemon opens once, seeks to end of last write, writes log info. If you truncate you get holes that count as NUL (Ascii 0) bytes if you cp for example. This often happens when the tool has different logging modes, and one of them is "circular logging". By the way renaming the file is of no use as the running process holds a descriptor to it. Even deleting does not work you'll get an hidden file (that is the old unix trick for having hidden temporaries, you open and unlink just after)
  • In some cases you can use the fifo trick. Stop the daemon, substitute the logfile for a fifo (some systems might even have a special "/dev/log" driver) and some reader daemon, that dumps a file every x bytes say (perl is perfect here but youŽll need a reader manager is you use the scheme with more than a few processes). Restart the daemon, if it starts logging you are lucky (some processes do a -f test, start complaining and bail out).

    update: corrected typos.

    hth --stephan

    In reply to Re: Emptying log file by sgt
    in thread Emptying log file by Anonymous Monk

    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.