You have a file (say syslog), it is open by a process (say logger) and logger is writing to it.

A second process (perl) comes along and renames the file to syslog.bak and opens a new file called syslog it then proceeds to read lines from syslog.bak and selectively copy some of them to syslog, Process logger is still writing to syslog.bak. The perl script finishes processing, closes syslog.

What have you achieved? The log file that logger is writing to still contains all the lines it had to start with. You have a new file with a subselection of those lines, and have possibly archived the rest somewhere, but the file logger is writing to is still as big as it was originally, and growing. You have no way to remove those lines you archived from the original logfile, and no way to persuade logger to use the new shortened version of the file without stopping and starting logger?

Below are the results I got when I tried this on my (Win32) system. Maybe the results are different on *nix, but the fact that the logger process continues to use the unchanged, unshortened version of the file isn't?

C:\test>type logger.pl #! perl -sw use strict; use POSIX 'strftime'; $|++; #! Sat Aug 31 23:13:46 CDT 2002 while (1) { sleep 1; print strftime( '%a %b %d %T %Z %Y',localtime), "@{[times]}", +$/; } C:\test>logger Mon Oct 07 BST 20020.18 0.04 0 0 Mon Oct 07 BST 20020.18 0.04 0 0 Mon Oct 07 BST 20020.18 0.04 0 0 Mon Oct 07 BST 20020.18 0.04 0 0 Mon Oct 07 BST 20020.18 0.04 0 0 ^C C:\test>^Clogger >log # different session C:\test>dir log Volume in drive C has no label. Volume Serial Number is 8C87-E163 Directory of C:\test 02/10/07 09:59p 980 log 1 File(s) 980 bytes 25,827,328 bytes free C:\test>dir log* Volume in drive C has no label. Volume Serial Number is 8C87-E163 Directory of C:\test 02/10/07 09:59p 1,120 log 02/10/07 07:17p 186 logger.pl 2 File(s) 1,306 bytes 25,827,328 bytes free C:\test>perl -wni.bak -e "print if /7/;" log Can't do inplace edit on log: File exists. C:\test>perl -wni -e "print if /7/;" log Can't do inplace edit without backup. C:\test>

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

In reply to Re: Re: Re: Re: Re: cleaning up logs by BrowserUk
in thread cleaning up logs by ddrumguy

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.