Convert system info and temp deletion cron jobs to webmin cron
authorJamie Cameron <jcameron@webmin.com>
Wed, 19 May 2010 00:38:40 +0000 (17:38 -0700)
committerJamie Cameron <jcameron@webmin.com>
Wed, 19 May 2010 00:38:40 +0000 (17:38 -0700)
cron/cron-lib.pl
cron/postinstall.pl
cron/tempdelete.pl [deleted file]
webmincron/webmincron-lib.pl

index 47d641a..215975e 100755 (executable)
@@ -1362,5 +1362,40 @@ if ($err) {
        }
 }
 
+=head2 cleanup_temp_files
+
+Called from cron to delete old files in the Webmin /tmp directory
+
+=cut
+sub cleanup_temp_files
+{
+# Don't run if disabled
+if (!$gconfig{'tempdelete_days'}) {
+       print STDERR "Temp file clearing is disabled\n";
+       return;
+       }
+if ($gconfig{'tempdir'} && !$gconfig{'tempdirdelete'}) {
+       print STDERR "Temp file clearing is not done for the custom directory $gconfig{'tempdir'}\n";
+       return;
+       }
+
+local $tempdir = &transname();
+$tempdir =~ s/\/([^\/]+)$//;
+if (!$tempdir || $tempdir eq "/") {
+       $tempdir = "/tmp/.webmin";
+       }
+
+local $cutoff = time() - $gconfig{'tempdelete_days'}*24*60*60;
+opendir(DIR, $tempdir);
+foreach my $f (readdir(DIR)) {
+       next if ($f eq "." || $f eq "..");
+       local @st = lstat("$tempdir/$f");
+       if ($st[9] < $cutoff) {
+               &unlink_file("$tempdir/$f");
+               }
+       }
+closedir(DIR);
+}
+
 1;
 
index 13e6e36..2c0543e 100755 (executable)
@@ -3,26 +3,14 @@ require 'cron-lib.pl';
 
 sub module_install
 {
-# Create a cron job to delete old files in /tmp/.webmin
+# Create a Webmin cron job to delete old files in /tmp/.webmin
 eval {
        $main::error_must_die = 1;
-       local @jobs = &list_cron_jobs();
-       local ($job) = grep { $_->{'user'} eq 'root' &&
-                             $_->{'command'} eq $temp_delete_cmd } @jobs;
-       if (!$job) {
-               $job = { 'user' => 'root',
-                        'active' => 1,
-                        'command' => $temp_delete_cmd,
-                        'mins' => int(rand()*60),
-                        'hours' => int(rand()*24),
-                        'days' => '*',
-                        'months' => '*',
-                        'weekdays' => '*',
-                        'comment' => 'Delete Webmin temporary files' };
-               &unconvert_comment($job);
-               &create_cron_job($job);
-               &create_wrapper($temp_delete_cmd, $module_name,"tempdelete.pl");
-               }
+       &foreign_require("webmincron");
+       local $cron = { 'module' => $module_name,
+                       'func' => 'cleanup_temp_files',
+                       'interval' => 60 };
+       &webmincron::create_webmin_cron($cron, $temp_delete_cmd);
        };
 if ($@) {
        print STDERR "Failed to setup /tmp cleanup cron job : $@\n";
diff --git a/cron/tempdelete.pl b/cron/tempdelete.pl
deleted file mode 100755 (executable)
index 41deeb2..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/usr/local/bin/perl
-# Delete any Webmin temp files older than 7 days
-
-$no_acl_check++;
-require './cron-lib.pl';
-
-if ($ARGV[0] eq "-debug" || $ARGV[0] eq "--debug") {
-       shift(@ARGV);
-       $debug = 1;
-       }
-
-# Don't run if disabled
-if (!$gconfig{'tempdelete_days'}) {
-       print "Temp file clearing is disabled\n";
-       exit(0);
-       }
-if ($gconfig{'tempdir'} && !$gconfig{'tempdirdelete'}) {
-       print "Temp file clearing is not done for the custom directory $gconfig{'tempdir'}\n";
-       exit(0);
-       }
-
-$tempdir = &transname();
-$tempdir =~ s/\/([^\/]+)$//;
-if ($debug) {
-       print "Checking temp directory $tempdir\n";
-       }
-
-$cutoff = time() - $gconfig{'tempdelete_days'}*24*60*60;
-opendir(DIR, $tempdir);
-foreach my $f (readdir(DIR)) {
-       next if ($f eq "." || $f eq "..");
-       local @st = lstat("$tempdir/$f");
-       if ($st[9] < $cutoff) {
-               if ($debug) {
-                       print "Deleting $tempdir/$f\n";
-                       }
-               &unlink_file("$tempdir/$f");
-               }
-       }
-closedir(DIR);
-
index cbfff48..1d1db13 100644 (file)
@@ -66,6 +66,7 @@ my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
 &lock_file($file);
 &write_file($file, $cron);
 &unlock_file($file);
+&reload_miniserv();
 }
 
 =head2 delete_webmin_cron(&cron)
@@ -80,6 +81,7 @@ my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
 &lock_file($file);
 &unlink_file($file);
 &unlock_file($file);
+&reload_miniserv();
 }
 
 =head2 create_webmin_cron(&cron, [old-cron-command])
@@ -99,9 +101,13 @@ my $already;
 foreach my $oc (@crons) {
        next if ($oc->{'module'} ne $cron->{'module'});
        next if ($oc->{'func'} ne $cron->{'func'});
+       my $sameargs = 1;
        for(my $i=0; defined($oc->{'arg'.$i}) ||
-                    defined($cron->{'arg'.$i}); $i++) { }
-       $already = $c;
+                    defined($cron->{'arg'.$i}); $i++) {
+               $sameargs = 0 if ($oc->{'arg'.$i} ne $cron->{'arg'.$i});
+               }
+       next if (!$sameargs);
+       $already = $oc;
        last;
        }
 if ($already) {