[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: standalone cron



No warranties are expressed or implied.  Basically, this is set to use the same folder that Cron (the Mac daemon) uses.  I save it as a standalone app called "CronPerl" and put it in the same folder as MacPerl.  It opens files and determines the cron data from the filename, as Cron does, and then uses do $file to execute the script when it is time to do so.

I use IO::Tee which is on CPAN, so I can save everything to a logfile and display to a window.  It may very well have bugs in it, and may not be very efficient, but it works for me.  One problem it has is that it cannot execute more than one script at once, so it tries to backtrack if it misses a minute.

Have fun, submit bug reports/fixes to me if you like.

#!perl
# crond by pudge@pobox.com 1998.07.07

use strict;
use Mac::Files;
use Time::Local;
use IO::Tee;
my($dir, $log, %files, @files);

$dir = FindFolder(kOnSystemDisk(), kPreferencesFolderType()) . ':Cron Preferences:';
$log = "${dir}log";
_init();

while (1) {
  my @time = localtime;
  @files = ();
  FILES: for my $file (keys %files) {
    my @nums = @{$files{$file}};
    for my $x (1 .. 4, 6) {
      if ($nums[$x] =~ /^(\d+)-?(\d+)?$/) {
        my $c;
        for ($1 .. $2 || $1) {$c++ if $_ == $time[$x]}
        next FILES if !$c;
      }
    }
    push @files, $file;
  }

  for my $file (@files) {
    print "[${\(scalar localtime)}]";
    printf "     $files{$file}[7] ... %s",
      do $file ? 'Done.' : "can't do $file:\n  $!";
  }

  if ((localtime)[1] != $time[1]) {
    $time[1]++;
    @files = ();
    print "Redo";
    do FILES;
  }

  sleep(61-(localtime)[0]);
}

sub _init {
  open(STDERR, ">&STDOUT") or die $!;
  open(LOG, ">>$log") or die $!;
  tie(*TEE, q(IO::Tee), *STDOUT, *LOG);
  select(TEE); $| = 1; $\ = "\n";
  printf "\n\nStarting at [${\(scalar localtime)}] ...\n";

  %files = ();
  opendir(D, $dir) or die $!;
  foreach my $file (sort readdir D) {
    if ($file =~ /^([0-9-]+|\*)\s+([0-9-]+|\*)\s+([0-9-]+|\*)\s+([0-9-]+|\*)\s+([0-9-]+|\*)\s+(.*)$/) {
      $files{"${dir}$file"} = [$file, $1, $2, $3, $4, undef, $5, $6];
      print $file;
    }
  }
  closedir(D);
  END {print "\nLog closed at [${\(scalar localtime)}]"}
}

__END__

-- 
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch