CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Opening a log file that is in use by another program



May 16, 2008, 5:02 PM

Post #1 of 2 (242 views)
Opening a log file that is in use by another program Can't Post


I'm trying to write a script that will analyze the content of a log file (not apache or anything that's available with many google searches). It cannot use tie::file or tail, and I'm running this with Activestate on Win32.

If I run the script below when the application that generates the log entries is NOT running, everything works fine. When the application is running, the script will only parse up until the time I start the script.

For example: The program has added 100 entries to the log. Then I start the script. It gets to line 100, then continues in the while loop, not reading any lines. Any new lines added by the program are not seen.

I realize that the problem may be with the $linecount portion, but I am unaware of any way to fix this. This is a very small log (rarely over 1MB), so I don't care about any unnecessary memory utilization.

Any help is greatly appreciated.

$logfile = 'C:\path\to\log\file.txt'; #replace this with the file location.  This is case sensitive. 
$batchfile = 'C:\path\to\batch\file.bat' #Replace this with the location of the batch file. This is case sensitive.
$found = 0;
$linecount = 0;

while ($found == 0) {
$pid = open (LOGFILE, "< $logfile");
@loglines = <LOGFILE>;
linecheck (@loglines[$linecount]);
close (LOGFILE);
sleep (20);

if ($found == 1) {
sleep (120);
unlink $logfile;

sub linecheck {
#Replace the text between // with the string to be matched.
if (@_[0] =~ /some string I'm searching for/i) {
system ($batchfile);
$found = 1;


May 18, 2008, 8:03 AM

Post #2 of 2 (223 views)
Re: [chaotic] Opening a log file that is in use by another program [In reply to] Can't Post

Not sure if it will help you are not, but look into the seek() function.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives