我们有一个日志阅读器脚本,例如:
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;
(代码由@duskast)
我们需要此代码每天自动运行,例如每天早上 7 点,以及每周自动运行。
我知道要每天运行这个命令,需要一个“cron”命令或一些 shell 脚本,因为我们在这里使用 Linux,但我从未使用过该命令。
另外,每周一次怎么样?这将是最近 7 天的总和,那么也许可以使用 Perl 来完成?
最佳答案
您应该使用 cron 和 crontab。
使用 cron 的标准格式是:
Minute Hour Day_of_Month Month Day_of_Week Cmd
正在运行
25 07 05 * * /home/user/log_reader.pl
将在每月 5 日(星号允许任何月份)任何工作日上午 7:25 运行(星号选择允许任何工作日)
所以你的 cron 工作..
00 6 * * * /home/user/log_reader.pl
每天早上 6:00 运行。
这个,
00 6 * * 3 /home/user/log_reader.pl
将在一周的第三天早上 6:00 运行,即每周运行一次。
希望有帮助。
关于linux - Perl 和 Linux? : Make a log reader script run daily and weekly (automatic),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932528/