http://www.perlmonks.org?node_id=168106
Category: Win32 Stuff
Author/Contact Info Oscar Alarcón R
Description: A Crond service, like Crond of Unix systems. its interpreted the crontab file. Enjoy !
package PerlSvc;
# CollapsedSubs: RDD_Crontab  EXE_CronTask
$Name        = 'S-Cron Daemon';
$DisplayName = 'Sinapse Cron Module Service';

sub Startup {
        while(ContinueRun()) {
                &main::STR_CronD;
                sleep(1);
        }
}
sub Install {
        print "Installing Sinapse Cron Module service\n";
        print "\nNote : Please verify the correct sintax in your cront
+ab\n";
}
sub Remove {
        print "Removing Sinapse Cron Module service\n";
}
sub Help {
        print "Help About Installation of Sinapse Cron Module service\
+n";
        print "\nFor normal instalation of this service, please type:\
+n";
        print "     cronsvc -install\n";
        print "For normal installation of the service whit auto startu
+p, please type :\n";
        print "     cronsvc -install auto\n";
        print "For more reference please consult with Sinapse Media Gr
+oup\n";
}

package main;

use Time::localtime;

my %dias =               (
                1        =>        "Lu",
                2        =>        "Ma",
                3        =>        "Mi",
                4        =>        "Ju",
                5        =>        "Vi",
                6        =>        "Sa",
                7        =>        "Do"
                );
my %dias_crontab =       (
                1        =>        "Lu",
                2        =>        "Ma",
                3        =>        "Mi",
                4        =>        "Ju",
                5        =>        "Vi",
                6        =>        "Sa",
                0        =>        "Do"
                );

sub STR_CronD {
        if(localtime->sec() eq 0){
                my(@cront)=&main::RDD_Crontab();
                foreach (@cront){
                        chomp($_);
                        my($minu, $hora, $din, $mes, $dia, $ejecuta)=s
+plit(/ /, $_,6);
                        if($dia eq "*"){
                                if($mes eq "*"){
                                        if($din eq "*"){
                                                if($hora eq "*"){
                                                        my(@minut)=spl
+it(/\,/, $minu);
                                                        foreach(@minut
+){
                                                                if($_ 
+eq localtime->min()){
                                                                      
+  &main::EXE_CronTask($ejecuta);
                                                                }
                                                        }
                                                }else{
                                                        my(@horat)=spl
+it(/\,/, $hora);
                                                        foreach(@horat
+){
                                                                if($_ 
+eq localtime->hour()){
                                                                      
+  my(@minut)=split(/\,/, $minu);
                                                                      
+  foreach(@minut){
                                                                      
+          if($_ eq localtime->min()){
                                                                      
+                  &main::EXE_CronTask($ejecuta);
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }
                                                }
                                        }else{
                                                my(@dint)=split(/\,/, 
+$din);
                                                foreach(@dint){
                                                        if($_ eq local
+time->mday()){
                                                                if($ho
+ra eq "*"){
                                                                      
+  my(@minut)=split(/\,/, $minu);
                                                                      
+  foreach(@minut){
                                                                      
+          if($_ eq localtime->min()){
                                                                      
+                  &main::EXE_CronTask($ejecuta);
                                                                      
+          }
                                                                      
+  }
                                                                }else{
                                                                      
+  my(@horat)=split(/\,/, $hora);
                                                                      
+  foreach(@horat){
                                                                      
+          if($_ eq localtime->hour()){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }
                                                }
                                        }
                                }else{
                                        my(@mest)=split(/\,/, $mes);
                                        foreach(@mest){
                                                if($_ eq (localtime->m
+on()+1)){
                                                        if($din eq "*"
+){
                                                                if($ho
+ra eq "*"){
                                                                      
+  my(@minut)=split(/\,/, $minu);
                                                                      
+  foreach(@minut){
                                                                      
+          if($_ eq localtime->min()){
                                                                      
+                  &main::EXE_CronTask($ejecuta);
                                                                      
+          }
                                                                      
+  }
                                                                }else{
                                                                      
+  my(@horat)=split(/\,/, $hora);
                                                                      
+  foreach(@horat){
                                                                      
+          if($_ eq localtime->hour()){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }else{
                                                                my(@di
+nt)=split(/\,/, $din);
                                                                foreac
+h(@dint){
                                                                      
+  if($_ eq localtime->mday()){
                                                                      
+          if($hora eq "*"){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }else{
                                                                      
+                  my(@horat)=split(/\,/, $hora);
                                                                      
+                  foreach(@horat){
                                                                      
+                          if($_ eq localtime->hour()){
                                                                      
+                                  my(@minut)=split(/\,/, $minu);
                                                                      
+                                  foreach(@minut){
                                                                      
+                                          if($_ eq localtime->min()){
                                                                      
+                                                  &main::EXE_CronTask
+($ejecuta);
                                                                      
+                                          }
                                                                      
+                                  }
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }else{
                                my(@diat)=split(/\,/, $dia);
                                foreach(@diat){
                                        if($dias_crontab{$_} eq $dias{
+localtime->wday()}){
                                                if($mes eq "*"){
                                                        if($din eq "*"
+){
                                                                if($ho
+ra eq "*"){
                                                                      
+  my(@minut)=split(/\,/, $minu);
                                                                      
+  foreach(@minut){
                                                                      
+          if($_ eq localtime->min()){
                                                                      
+                  &main::EXE_CronTask($ejecuta);
                                                                      
+          }
                                                                      
+  }
                                                                }else{
                                                                      
+  my(@horat)=split(/\,/, $hora);
                                                                      
+  foreach(@horat){
                                                                      
+          if($_ eq localtime->hour()){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }else{
                                                                my(@di
+nt)=split(/\,/, $din);
                                                                foreac
+h(@dint){
                                                                      
+  if($_ eq localtime->mday()){
                                                                      
+          if($hora eq "*"){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }else{
                                                                      
+                  my(@horat)=split(/\,/, $hora);
                                                                      
+                  foreach(@horat){
                                                                      
+                          if($_ eq localtime->hour()){
                                                                      
+                                  my(@minut)=split(/\,/, $minu);
                                                                      
+                                  foreach(@minut){
                                                                      
+                                          if($_ eq localtime->min()){
                                                                      
+                                                  &main::EXE_CronTask
+($ejecuta);
                                                                      
+                                          }
                                                                      
+                                  }
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }
                                                }else{
                                                        my(@mest)=spli
+t(/\,/, $mes);
                                                        foreach(@mest)
+{
                                                                if($_ 
+eq (localtime->mon()+1)){
                                                                      
+  if($din eq "*"){
                                                                      
+          if($hora eq "*"){
                                                                      
+                  my(@minut)=split(/\,/, $minu);
                                                                      
+                  foreach(@minut){
                                                                      
+                          if($_ eq localtime->min()){
                                                                      
+                                  &main::EXE_CronTask($ejecuta);
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }else{
                                                                      
+                  my(@horat)=split(/\,/, $hora);
                                                                      
+                  foreach(@horat){
                                                                      
+                          if($_ eq localtime->hour()){
                                                                      
+                                  my(@minut)=split(/\,/, $minu);
                                                                      
+                                  foreach(@minut){
                                                                      
+                                          if($_ eq localtime->min()){
                                                                      
+                                                  &main::EXE_CronTask
+($ejecuta);
                                                                      
+                                          }
                                                                      
+                                  }
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }else{
                                                                      
+          my(@dint)=split(/\,/, $din);
                                                                      
+          foreach(@dint){
                                                                      
+                  if($_ eq localtime->mday()){
                                                                      
+                          if($hora eq "*"){
                                                                      
+                                  my(@minut)=split(/\,/, $minu);
                                                                      
+                                  foreach(@minut){
                                                                      
+                                          if($_ eq localtime->min()){
                                                                      
+                                                  &main::EXE_CronTask
+($ejecuta);
                                                                      
+                                          }
                                                                      
+                                  }
                                                                      
+                          }else{
                                                                      
+                                  my(@horat)=split(/\,/, $hora);
                                                                      
+                                  foreach(@horat){
                                                                      
+                                          if($_ eq localtime->hour())
+{
                                                                      
+                                                  my(@minut)=split(/\
+,/, $minu);
                                                                      
+                                                  foreach(@minut){
                                                                      
+                                                          if($_ eq lo
+caltime->min()){
                                                                      
+                                                                  &ma
+in::EXE_CronTask($ejecuta);
                                                                      
+                                                          }
                                                                      
+                                                  }
                                                                      
+                                          }
                                                                      
+                                  }
                                                                      
+                          }
                                                                      
+                  }
                                                                      
+          }
                                                                      
+  }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
        return();
}


sub RDD_Crontab {
        my @cront1 = ();
        open (CRON, "< c:\\sinapse\\cfg\\crontab");
        while (<CRON>){
                chomp($_);
                push (@cront1, $_);
        }
        close(CRON);
        return(@cront1);
}

sub EXE_CronTask {
       my($action)=(@_);
       system($action);
       return();
}



# Crontab indice
#
# MINUTE(0-59)    HOUR(0-23)     DAYOFMONTH(1-31)    MONTHOFYEAR(1-12)
+     DAYOFWEEK(0-6)  Note 0 = Sun
#                                     *                   *           
+            *
# ejp
# 0 0 1 1 * task