Change Scheduled Time Using Schedule :: Cron

I am writing a script in Perl that needs to be run at the same time every night, unless it needs to be changed. I found Schedule :: Cron in CPAN and it does what I want it to do. According to the documentation for the run method,

nofork => 1

Do not start when the scheduler starts. Instead, jobs are performed in the current process. In the tasks you perform, you have full access to the global variables of your script and, therefore, can affect other tasks that are performed at other times.

This is what I want to do, but it is not. Whenever I look at the memory location of global variables, they are the same, but when the task starts, the value does not change.

I ran this on both Windows and Linux, and I had someone else looking at the code to make sure my logic is correct. What do I need to do to save changes to global variables.

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";
}
+3
source share
1 answer

There is a significant difference between no_forkand nofork. Try:

$cron->run(nofork => 1);
+7
source

Source: https://habr.com/ru/post/1788294/


All Articles