gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:11:08 25 4
gpt4 key购买 nike

我正在用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com