-2

We have a Log reader script, for example:

use strict;
use warnings;

my $location = "file.txt";
open LOGFILE, $location;

my $first_line = 1;
my $max_id;

while (<LOGFILE>) {
    if (/item_id:(\d)+/) {
        if ($first_line) {
            $first_line = 0;
            $max_id = $1;
        } else {
            $max_id = $1 if ($1 > $max_id);
        }
    }
}

my $found = $max_id;
print "$found\n";

close LOGFILE;

(code by @duskast)

And we need this code to run automatically on a daily basis, say every day at 7 am, and also on a weekly basis.

I know that to run this daily there is a "cron" command or some shell script, as we are using linux here, but I have never used that command.

Also, how about weekly? That would be the sum of the latest 7 days, so perhaps that can be done with Perl?

mpapec
  • 50,217
  • 8
  • 67
  • 127
ado
  • 1,451
  • 3
  • 17
  • 27
  • 4
    If you have never used the `cron` command, you can still learn about it by typing `man cron` in your shell. – devnull Jun 05 '13 at 05:59
  • 1
    And/or `man crontab` (since you submit jobs to `cron` via `crontab`). – Jonathan Leffler Jun 05 '13 at 06:05
  • With no command line arguments, the 'weekly' version is going to do the same processing as the 'daily' version. Unless you might have negative or zero item IDs, you could dramatically simplify the loop logic with: `my $max_id = 0; while (<>) { $max_id = $1 if (m/item_id:(\d+)/ && $1 > $max_id); }`. This reads files specified on the command line or standard input. – Jonathan Leffler Jun 05 '13 at 06:09
  • @JonathanLeffler thanks, yes in this case the weekly will do the same as the daily version. but assume a more general case, a script that stores info to a database, including the "time" where the storage took place in unix time. In this case, if we want a weekly version how can we do it? I know that a daily one is just adding: 00 6 * * * /home/user/log_reader.pl to cron file – ado Jun 05 '13 at 06:20
  • 1
    Daily: `m h * * * cmd`. Weekly: `m h * * dow cmd` (`man 5 crontab`) – ikegami Jun 05 '13 at 06:25
  • @ikegami thanks, so just for confirming here, imagine we run this weekly and daily at the same time: 00 6 * * * /home/user/log_reader.pl 00 6 * * mon /home/user/log_reader.pl and the script is a log reader that stores info to a mysql table woulnt there be a conflict every monday at 6 00 am? – ado Jun 05 '13 at 06:28
  • It's a number from 0 to 6. `man 5 crontab`. And no, your machine is quite capable of running two programs at the same time. (There are dozens running at any given time!) – ikegami Jun 05 '13 at 06:30
  • @ikegami thanks i checked man 5 crontab. its 1 instead of monday. – ado Jun 05 '13 at 06:35
  • 1
    oh, didn't notice they're the same script. Yes, it would be dumb to create a weekly entry that runs the same program as a daily entry. The weekly entry would be fully redundant. – ikegami Jun 05 '13 at 06:37

1 Answers1

4

cron and crontab is what you should use.

the standard format for using cron is:

    Minute Hour Day_of_Month Month Day_of_Week Cmd

so running

    25 07 05 * * /home/user/log_reader.pl

will run 7:25am on the 5th of each month (asterisk allows any month) any weekday (asterisk selectts any week day allowed)

So your cron job..

    00 6 * * * /home/user/log_reader.pl

is running 6:00am each_month, everyday.

this one,

    00 6 * * 3 /home/user/log_reader.pl

will run 6:00am on 3rd day of the week, meaning once a week.

Hope that helps.

Harry Barry
  • 194
  • 7