Gossamer Forum
Home : General : Perl Programming :

How to continuously read a file after every 5 mins

Quote Reply
How to continuously read a file after every 5 mins
  
Hi,

Suppose I am reading a text file in my perl script which is continuously getting updated.
How can I read that text file after a particular time (in every 5 or 6 mins) continuously so that my code automatically parses the updated text file.

Thanks.
Quote Reply
Re: [xaverian] How to continuously read a file after every 5 mins In reply to
Its a bit hard really... you can't use a cronjob to do it? (set to run every 5 mins) ?

Otherwise, you need to do something like:

Code:
sub main {

my $start = CORE::time();
# do your stuff here
my $end = CORE::time();

my $time_taken = $end - $start; # gives us the number of seconds it took to run...

my $secs_to_sleep = (5 * 60) - $time_taken; # 5 mins worth of seconds, minus the time already taken

# then do a "sleep" to make it wait until that time has come...
sleep $secs_to_sleep;
}

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [xaverian] How to continuously read a file after every 5 mins In reply to
Mmmm not entirely tested as I'm slicing this from similar job, but give it a try:

Code:
#!/usr/bin/perl

$logfile = "your_log_file.log";

open $log, '<', $logfile;
my $position;

while (1) {
while (my $line = <$log>) {

# Skip the line if it's being writen atm
last unless substr($line, -1) eq "\n";

# Do some stuff here

# When finished, save the postion for later
$position = tell $log;
}

# Sleep
sleep (300); # Sleep for 5 min

# Skip the lines we already parsed
seek $log, $position, 0;
}

The script will parse the file then save the current position and sleep for 300 sec (5 mins).

Hope this helps.

Cheers,
Boris

Facebook, Twitter and Google+ Auth for GLinks and GCommunity | reCAPTCHA for GLinks | Free GLinks Plugins

Last edited by:

eupos: Feb 8, 2013, 6:24 AM