Work on standard time specs for webmin cron
authorJamie Cameron <jcameron@webmin.com>
Sat, 29 May 2010 19:50:31 +0000 (12:50 -0700)
committerJamie Cameron <jcameron@webmin.com>
Sat, 29 May 2010 19:50:31 +0000 (12:50 -0700)
miniserv.pl
time/sync.pl [deleted file]
time/time-lib.pl
webmincron/webmincron-lib.pl

index 8da3d7b..a307be7 100755 (executable)
@@ -4828,13 +4828,23 @@ sub execute_ready_webmin_crons
 my $now = time();
 my $changed = 0;
 foreach my $cron (@webmincrons) {
+       my $run = 0;
        if (!$webmincron_last{$cron->{'id'}}) {
                # If not ever run before, don't run right away
                $webmincron_last{$cron->{'id'}} = $now;
                $changed = 1;
                }
-       elsif ($now - $webmincron_last{$cron->{'id'}} > $cron->{'interval'}) {
+       elsif ($cron->{'interval'} &&
+              $now - $webmincron_last{$cron->{'id'}} > $cron->{'interval'}) {
                # Older than interval .. time to run
+               $run = 1;
+               }
+       elsif ($cron->{'mins'}) {
+               # Check if current time matches spec
+               my @tm = localtime($now);
+               }
+
+       if ($run) {
                print DEBUG "Running cron id=$cron->{'id'} ".
                            "module=$cron->{'module'} func=$cron->{'func'}\n";
                $webmincron_last{$cron->{'id'}} = $now;
@@ -4930,14 +4940,68 @@ foreach my $f (readdir(CRONS)) {
                &read_file("$config{'webmincron_dir'}/$f", \%cron);
                $cron{'id'} = $1;
                my $broken = 0;
-               foreach my $n ('interval', 'module', 'func') {
+               foreach my $n ('module', 'func') {
                        if (!$cron{$n}) {
-                               print STDERR "Cron $1 missing interval\n";
+                               print STDERR "Cron $1 missing $n\n";
                                $broken = 1;
                                }
                        }
-               print DEBUG "adding cron id=$cron{'id'} module=$cron{'module'} func=$cron{'func'}\n";
-               push(@webmincrons, \%cron) if (!$broken);
+               if (!$cron{'interval'} && !$cron{'mins'} && !$cron{'special'}) {
+                       print STDERR "Cron $1 missing any time spec\n";
+                       $broken = 1;
+                       }
+               if ($cron{'special'} eq 'hourly') {
+                       # Run every hour on the hour
+                       $cron{'mins'} = 0;
+                       $cron{'hours'} = '*';
+                       $cron{'days'} = '*';
+                       $cron{'months'} = '*';
+                       $cron{'weekdays'} = '*';
+                       }
+               elsif ($cron{'special'} eq 'daily') {
+                       # Run every day at midnight
+                       $cron{'mins'} = 0;
+                       $cron{'hours'} = '0';
+                       $cron{'days'} = '*';
+                       $cron{'months'} = '*';
+                       $cron{'weekdays'} = '*';
+                       }
+               elsif ($cron{'special'} eq 'monthly') {
+                       # Run every month on the 1st
+                       $cron{'mins'} = 0;
+                       $cron{'hours'} = '0';
+                       $cron{'days'} = '1';
+                       $cron{'months'} = '*';
+                       $cron{'weekdays'} = '*';
+                       }
+               elsif ($cron{'special'} eq 'weekly') {
+                       # Run every month on the 1st
+                       $cron{'mins'} = 0;
+                       $cron{'hours'} = '0';
+                       $cron{'days'} = '*';
+                       $cron{'months'} = '*';
+                       $cron{'weekdays'} = '0';
+                       }
+               elsif ($cron{'special'} eq 'yearly' ||
+                      $cron{'special'} eq 'annually') {
+                       # Run every year on 1st january
+                       $cron{'mins'} = 0;
+                       $cron{'hours'} = '0';
+                       $cron{'days'} = '1';
+                       $cron{'months'} = '1';
+                       $cron{'weekdays'} = '*';
+                       }
+               else {
+                       print STDERR "Cron $1 invalid special time $cron{'special'}\n";
+                       $broken = 1;
+                       }
+               if ($cron{'special'}) {
+                       delete($cron{'special'});
+                       }
+               if (!$broken) {
+                       print DEBUG "adding cron id=$cron{'id'} module=$cron{'module'} func=$cron{'func'}\n";
+                       push(@webmincrons, \%cron);
+                       }
                }
        }
 }
diff --git a/time/sync.pl b/time/sync.pl
deleted file mode 100755 (executable)
index a8fa23b..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/usr/local/bin/perl
-# sync.pl
-# Sync with a time server, from cron
-
-$no_acl_check++;
-require './time-lib.pl';
-
-$err = &sync_time($config{'timeserver'}, $config{'timeserver_hardware'});
-if ($err) {
-       print STDERR $err;
-       exit(1);
-       }
-exit(0);
index cd13d2d..2df9489 100755 (executable)
@@ -67,6 +67,14 @@ if ($_[1]) {
 return undef;
 }
 
+# sync_time_cron()
+# Called from webmin cron to sync from the configured server
+sub sync_time_cron
+{
+my $err = &sync_time($config{'timeserver'}, $config{'timeserver_hardware'});
+print STDERR $err;
+}
+
 sub has_timezone
 {
 return 0 if (!defined(&list_timezones));
index 1d1db13..1906d93 100644 (file)
@@ -30,7 +30,19 @@ with keys :
 
 =item arg0,arg1,etc.. - Strings to pass to the function as parameters
 
-=item interval - Number of seconds between runs
+=item interval - Number of seconds between runs (optional)
+
+=item mins - Minutes on which to run. Can be * or a comma-separated list
+
+=item hours - Hours on which to run
+
+=item days - Days of the month on which to run
+
+=item months - Months of the year on which to run
+
+=item weekdays - Days of week on which to run
+
+=item special - Can be 'hourly', 'daily', 'weekly' or 'monthly'
 
 =cut
 sub list_webmin_crons