perl - 使用 Schedule::Cron 更改预定时间

标签 perl perl-module scheduled-tasks

我正在用 Perl 编写脚本,需要每晚在同一时间运行,但有时时间需要更改。我找到了 Schedule::Cron在 CPAN 上,它做我想做的事。根据 run 方法的文档,

nofork => 1

Don't fork when starting the scheduler. Instead, the jobs are executed within current process. In your executed jobs, you have full access to the global variables of your script and hence might influence other jobs running at a different time.

这是我想做的,但没有做。每当我检查全局变量内存位置时,它们都是相同的,但任务启动时值不会改变。

我在 Windows 和 Linux 上都运行过这个程序,我让其他人查看代码以查看我的逻辑是否正确。我需要做什么来保留对全局变量的更改。

use warnings;
use strict;

use Schedule::Cron;
use Time::localtime;

use constant {
    EVERY_DAY_10PM => '* * * * * 4,16,28,40,52',
    EVERY_DAY_NOON => '* * * * * 0,12,24,36,48',
    EVERY_DAY_2AM => '* * * * * 7,19,31,43,55'
};

############GLOBAL VARIABLES############
our $cron = new Schedule::Cron(\&runUpdate);
our $cronId;
our $updateTimeDirty = 0;
############END GLOBAL VARIABLES############

############MAIN PROGRAM BODY############
$cronId = $cron->add_entry(EVERY_DAY_10PM);#defaults to \&runUpdate
$cron->add_entry(EVERY_DAY_NOON, \&changeTime);
$cron->run(no_fork => 1);
############END MAIN PROGRAM BODY############

sub changeTime {
    our $cron;
    our $cronId;
    our $updateTimeDirty;

    print "updateTimeDirty is $updateTimeDirty\n";
    print "udpateTimeDirty location: " . \$updateTimeDirty . "\n";
    print "cron object: " . \$cron . "\n";

    if ($updateTimeDirty) {
        my $cronEntry = $cron->get_entry($cronId);
        $cronEntry->{time} = EVERY_DAY_2AM;
        $cron->update_entry($cronId, $cronEntry);
    }
    print "\n";
}

sub runUpdate {
    our $updateTimeDirty;

    $updateTimeDirty = 1;
    print "Updating at " . localtime()->sec . " ($updateTimeDirty)\n\n";
}

最佳答案

no_forknofork 之间存在显着差异。尝试:

$cron->run(nofork => 1);

关于perl - 使用 Schedule::Cron 更改预定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820733/

相关文章:

perl - 如何在运行时加载 Perl 模块?

perl - 如何使用Perl的XML::LibXML提取标记中的属性?

perl - mod_perl 与 mod_fastcgi

regex - 匹配任何 URL 参数值的正则表达式(Perl 语法)

perl - 为什么 sub 中的 use 语句全局适用?

regex - Perl,如何确定变量值是否为数字?

ruby-on-rails - Resque .. 我怎样才能得到队列列表

windows - 草莓 Perl : forgets environment variables?

scala - Scala 中的预定执行器

Windows 任务计划程序中带有 $_GET 变量的 PHP 脚本