Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Crond

by zeroquo (Novice)
on May 21, 2002 at 12:57 UTC ( [id://168106]=sourcecode: print w/replies, xml ) Need Help??
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
Replies are listed 'Best First'.
Re: Crond
by osfameron (Hermit) on May 21, 2002 at 13:46 UTC
    A few comments after a quick scan through the code:
    • I'm not sure how you are supposed to install this as a Win32 service (The supplied subs, "Remove", "Install", etc. don't seem to have anything pointing to them).
    • Some of this code looks like it should be refactored: e.g. the code for minutos and horas are almost identical, and with some re-writing you could have much simpler code.
    • You're not useing strict or warnings!
    • The name of the crontab file is hard-coded in the script. You should probably document that!
    • (And you should probably document that the names of the days are hard-coded in Spanish, and where to change them. If you've not learned how to use POD documentation try reading the perlpod manpage, very useful!)
    Hope that helps.

    Cheerio!
    Osfameron
    http://osfameron.perlmonk.org/chickenman/

      The code its make for Perl Dev Kit of ActiveState, its make an binary distribution of this code as native W32 service. Its announce, the only don´t use the Strict package and Warning package, because the module Perlsvc its fail on compile the source. That work similar that use Strict package, whit the use of "my" attribute for variable definition. Probe that , generating the binary file whith perlsvc compile package of ActiveState. Enjoy & Thanks !
Re: Crond
by belg4mit (Prior) on May 26, 2002 at 16:28 UTC
    For example you could do
    my(%dias, %dias_crontab); %dias = %dias_crontab = ( ... );
    Or alternately, simply only use one hash, since the data is the same.

    Your regexps for extracting field data are actually too strict, you do not allow for whitespace between commas, or for non space whitespace.

    You might want to take a glance at "a cron; in perl: But Why?!", and perhaps consider wrapping it in a shell to interface with Windows services?

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://168106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found