$run = 1;
}
elsif ($cron->{'mins'}) {
- # Check if current time matches spec
+ # Check if current time matches spec, and we haven't run in the
+ # last minute
my @tm = localtime($now);
+ if (&matches_cron($cron->{'mins'}, $tm[1]) &&
+ &matches_cron($cron->{'hours'}, $tm[2]) &&
+ &matches_cron($cron->{'days'}, $tm[3]) &&
+ &matches_cron($cron->{'months'}, $tm[4]+1) &&
+ &matches_cron($cron->{'weekdays'}, $tm[6]) &&
+ $now - $webmincron_last{$cron->{'id'}} > 60) {
+ $run = 1;
+ }
}
if ($run) {
}
}
+# matches_cron(cron-spec, time)
+# Checks if some minute or hour matches some cron spec, which can be * or a list
+# of numbers.
+sub matches_cron
+{
+my ($spec, $tm) = @_;
+if ($spec eq '*') {
+ return 1;
+ }
+else {
+ foreach my $s (split(/,/, $spec)) {
+ if ($s == $tm ||
+ $s =~ /^(\d+)\-(\d+)$/ && $tm >= $1 && $tm <= $2) {
+ return 1;
+ }
+ }
+ return 0;
+ }
+}
+
# read_webmin_crons()
# Read all scheduled webmin cron functions and store them in the @webmincrons
# global list
$cron{'months'} = '1';
$cron{'weekdays'} = '*';
}
- else {
+ elsif ($cron{'special'}) {
print STDERR "Cron $1 invalid special time $cron{'special'}\n";
$broken = 1;
}
require 'time-lib.pl';
-# Change time sync jobs running at midnight to a random time, to stop
-# overloading public NTP servers
+# Convert existing cron job to webmin cron
sub module_install
{
&foreign_require("cron", "cron-lib.pl");
local $job = &find_cron_job();
-if ($job && $job->{'mins'} eq '0' && $job->{'hours'} eq '0') {
- # Midnight .. fix it
- &seed_random();
- $job->{'mins'} = int(rand()*60);
- $job->{'hours'} = int(rand()*24);
- &cron::change_cron_job($job);
+if ($job) {
+ &foreign_require("webmincron");
+ $wcron = { 'module' => $module_name,
+ 'func' => 'sync_time_cron',
+ 'special' => $job->{'special'},
+ 'mins' => $job->{'mins'},
+ 'hours' => $job->{'hours'},
+ 'days' => $job->{'days'},
+ 'months' => $job->{'months'},
+ 'weekdays' => $job->{'weekdays'},
+ };
+ &webmincron::create_webmin_cron($wcron, $job->{'command'});
}
}