Agnus Dei (jackal) wrote,
Agnus Dei
jackal

Save time between two runs of a perl script

#!/usr/bin/perl
#
# I need to run SQL to process between two time stamps (in milliseconds) and to prevent processing
# the same data twice, i save the time of the last run and only use the time BETWEEN last run and this run
# but to do that I need to save the time of the last run of this script.  This code does that.
#
use Storable qw/store retrieve/;


my $debug=1;
my $last_run_time;                     # Read this from a saved file
my $this_run_time=`/bin/date +%s%3N`;  # Now time in miliseconds since 1970
chomp ($this_run_time);
my $run_time_file="/var/tmp/this_run_time.dat";

if ( -e $run_time_file ) {
   print "DEBUG: reading in last run time file data\n" if ($debug);
   $last_run_time = $ {retrieve $run_time_file} ;
} else {
   print "DEBUG: no last run time file data to read in. Subtracting 2 hours from now\n" if ($debug);
   $last_run_time = $this_run_time -120000 ;  # subtract 2 hours from now (in milliseconds) if this is the first time running

}

# Debug info
print "DEBUG: LAST RUN TIME = $last_run_time , THIS RUN TIME = $this_run_time \n" if ($debug);

# store the time so we only look at the time since last run
defined store \$this_run_time, $run_time_file or die( "could not save time to '$run_time_file'\n" );
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 1 comment