in reply to Re: Re: Re: Re: cleaning up logs
in thread cleaning up logs

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!

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: cleaning up logs
by gnu@perl (Pilgrim) on Oct 08, 2002 at 14:01 UTC
    K, I understand where you're coming from. I should have made myself more clear in my original post. I apologize for that. You are correct, the original process will continue to write to the logfile regardless of what you rename it to and data will be lost in the process of doing this.

    After the write that is occuring the next write by the logger will occur in the newly created file. But again, you have still lost the data that went to the old file after the rename.

      After the write that is occuring the next write by the logger will occur in the newly created file. But again, you have still lost the data that went to the old file after the rename.

      Only if the writing process closes and reopens the file. As long as the original process keeps the filehandle open, it'll write to the old file.

      -- Dan