Perl truncate problem

  • Follow


Problem:
--------
I have a log file (syslog-ng_msgs) which is being written to by a
system process (syslog-ng).
I have written a utility which I want to run as a cron job that will
copy the contents of "syslog-ng_msgs" to an archive file and then
truncate the
"syslog-ng" file.  All of this works fine except that the first time
"syslog-ng" writes an entry to the "syslog-ng_msgs" file after the
file has been truncated,
the first entry is there but is preceeded with garbage.

For instance, before truncation, syslog-ng_msgs would look like this:
Test line 1
Test line 2
Test line 3
Test line 4
Test line 5

After my utility is run, the "syslog-ng_msgs" file is truncated and
the "syslog-ng" process continues to write to this file, however
notice the garbage
preceeding the first entry as shown below:

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@Test line 6
Test line 7
Test line 8
Test line 9
Test line 10
Test line 11

Question:
---------
How do I get rid of that garbage?
Why is it there?
What concept am I missing?

Here is the code for my utility:
--------------------------------
#!/usr/bin/perl
 
my $syslogng_file = "/home/gage/Event_Archiver/syslog-ng_msgs";
 
my $syslogng_arch = "/home/gage/Event_Archiver/syslogng_msgs.arch";
 
open F_ARCH, "> $syslogng_arch"
      or die "Can't open $syslogng_arch for output; error $ERRNO";
 
open F_SYS, "+< $syslogng_file"
            or die "Can't open $syslogng_file for input; error
$ERRNO";
 
while (defined($s = <F_SYS>))
{
   print F_ARCH ("$s");
}
 
truncate(F_SYS,0);
close F_ARCH;
close F_SYS;

Things to keep in mind:
-----------------------
1.) I can't modify the "syslog-ng" process.
2.) I am runnning on Linux and could use the logrotate utility which I
am using
    using for other system apps but this utility that I am writing
needs to
    run on systems other than Linux so I don't want to make it
dependent on a
    Linux utility.

Thanks in advance for any and all comments or suggestions.
Regards,
--Eric
0
Reply eric.martin (3) 8/18/2004 2:54:40 PM


0 Replies
30 Views

(page loaded in 0.023 seconds)

Similiar Articles:





7/26/2012 7:01:02 PM


Reply: