Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: Search logs with crontab

by andal (Hermit)
on Nov 19, 2013 at 09:59 UTC ( #1063302=note: print w/replies, xml ) Need Help??

in reply to Re^2: Search logs with crontab
in thread Search logs with crontab

The idea is, you save the size of the file that you've checked at one point of time, and then next time you skip this much of data before you start looking for your string. Then you don't need to modify anything in the file.

The easiest way to do it is to let perl run forever, letting it sleep for 2 hours and then repeating the loop. Then you can keep the old file size simply in a variable. Beware, if you restart the script, you'll loose the offset.

If you insist on running perl from crontab, then you'll have to save the size in some file and then on startup read that file to obtain the offset of data where searching should start.

Note, there's still one catch here. Most likely the log file is rotated periodically, so your program should be smart enough to notice that log file was rotated and reset the saved offset.

As to your original approach with replacing strings, this also can be done, but this requires opening file in "read-write" mode open(FILE, "+<mylog.file"). Then you would mark position of the string which you want to replace, then seek to that position using seek function and then write the new string. Though you should be careful not to change the length of the string, otherwise you'll corrupt other data as well. After you are done writing, you would seek again to the position where you stopped reading and continue.

As you can see, that approach is somewhat complex, plus it is inefficient, because you'll have to scan through the same areas multiple times. The only benefit here is automatic handling of log rotation.

Replies are listed 'Best First'.
Re^4: Search logs with crontab
by taint (Chaplain) on Nov 19, 2013 at 15:44 UTC
    "As to your original approach with replacing strings, this also can be done, but this requires opening file in "read-write" mode open(FILE, "+<mylog.file")."

    There's also a potential "gotcha" here. That being; permissions. The subject file (in +RW) will require the maintenance of not only the current RWX bits, but that the script implementer has sufficient rights, and doesn't change ownership of said file.

    All in all, seems keeping markers in a separate file would probably be the most sensible approach -- FAR less overhead.


    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063302]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2018-06-24 22:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.