On Thu, 11 Aug 2005, Binish A R wrote: > Tony Frasketi wrote: > > > Hello Listers > > > > Is there any way in perl to effectively clear the contents of an > > ***existing*** file (making the file empty) ? I have a log file > > that I'd like to empty out after it reaches a certain maximum file > > size. > > > > thanks in advance > > Tony > > > just opening the file in write mode will empty the file .. > > open FD, ">filename"; But if other processes currently have the file open, this may not necessarily work: the contents of the file will be zeroed out, but the file will still consume however much disc space it had previously.
To do this the right way, the usual strategy is to halt any processes that would be writing to the log file, then move (or delete) it, then set up a new log file and set the processes running again. For an example of a well-tested approach to this problem, take a look at Apache's rotatelogs command. It's in C, but there's documentation for it all over the place, including the manpage. For similar functionality, there's also a standalone logrotate command in Debian et al that would also be enlightening to look at. For that matter, you could just as well use one of these commands as is rather than come up with your own, rounder approach to the same wheel invention process :-) -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>