user2201935
user2201935

Reputation: 396

Set start date and end date for cron script manually

I have a cron script for retrieving a report through an API which is scheduled to cron everyday and it retrieves the report of the previous day activity.

my $date = time2str('%Y-%m-%d', ( time - 86400 ));

my %api = ( 
           'example'  => {  
                          'example1' => 'example2',
          },
           'example3' => { 
                          'example4' => 'example5',
          },
);

foreach my $example (keys %api) {
    my $file_name = $api{$example}{example}."&date=" . $date;

    &function( $file_name, $example, $date );
}

sub function {
    my ($file_name, $example, $date) = @_;

    my $file_name = $example . $date . ".csv";

    open (FH, ">$file_name") || print "unable to write $!";

    close(FH);
}

But now I want to run the script manually from start date 15-05-2014 to end date 15-07-2014. How to do that?

Upvotes: 1

Views: 100

Answers (1)

Tudor Constantin
Tudor Constantin

Reputation: 26861

I think you'd have to modify that $date variable for all the days you want to run the script:

my %api = ( 
           'example'  => {  
                          'example1' => 'example2',
          },
           'example3' => { 
                          'example4' => 'example5',
          },
);


use DateTime;
my $date = DateTime->new(
    year       => 2014,
    month      => 5,
    day        => 15,
);

while ( $date->ymd le '2015-07-15' ){

  foreach my $example (keys %api) {
      my $file_name = $api{$example}{example}."&date=" . $date;

      &function( $file_name, $example, $date );
  }

  $date = $date->add(days => 1);

}
sub function {
    my ($file_name, $example, $date) = @_;

    my $file_name = $example . $date . ".csv";

    open (FH, ">$file_name") || print "unable to write $!";

    close(FH);
}

Upvotes: 1

Related Questions