linux - Perl 和 Linux? : Make a log reader script run daily and weekly (automatic)

标签 linux perl centos cron cron-task

我们有一个日志阅读器脚本,例如:

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/

相关文章:

linux - Linux gcc 支持 coreleft 函数吗?

c - 从recvfrom()缓冲区访问值

javascript - 使用 encode_json 后重音字符被双重编码

database - Perl 性能缓慢、文件 I/O 问题或由于 while 循环

linux - 问题 "Could not fetch/save url https://download.docker.com/linux/centos/docker-ce.repo"

linux - 如何知道哪个是当前内核模块?

linux - 在 Beagleboard-xm 中打开 CV 和 ubuntu 优化

perltex 全局使用严格

linux - 如何使 venv 完全可移植?

linux - 在两台机器之间引用私有(private) SSH key