2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
20 use vars qw{@crontab $mtime $lasttime $lastmin};
28 my $fn = "$main::cmd/crontab";
29 my $localfn = "$main::localcmd/crontab";
31 # cron initialisation / reading in cronjobs
34 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
39 # first read in the standard one
44 $mtime = $t if $t <= $mtime;
47 # then read in any local ones
52 $mtime = $t if $t <= $mtime;
61 my $fh = new FileHandle;
64 dbg('cron', "cron: reading $fn\n");
65 open($fh, $fn) or confess("cron: can't open $fn $!");
69 next if /^\s*#/o or /^\s*$/o;
70 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
75 $err |= parse($ref, 'min', $min, 0, 60);
76 $err |= parse($ref, 'hour', $hour, 0, 23);
77 $err |= parse($ref, 'mday', $mday, 1, 31);
78 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
79 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
83 dbg('cron', "cron: adding $_\n");
85 dbg('cron', "cron: error on line $line '$_'\n");
106 # handle comma delimited values
107 my @comma = split /,/o, $val;
109 my @minus = split /-/o;
111 return 1 if $minus[0] < $low || $minus[0] > $high;
112 return 1 if $minus[1] < $low || $minus[1] > $high;
114 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
118 return 1 if $_ < $low || $_ > $high;
122 $ref->{$sort} = \@req;
127 # process the cronjobs
130 my $now = $main::systime;
131 return if $now-$lasttime < 1;
133 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
135 # are we at a minute boundary?
136 if ($min != $lastmin) {
138 # read in any changes if the modification time has changed
141 $mon += 1; # months otherwise go 0-11
143 foreach $cron (@crontab) {
144 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
145 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
146 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
147 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
148 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
151 dbg('cron', "cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'");
153 dbg('cron', "cron: cmd error $@") if $@;
159 # remember when we are now
165 # these are simple stub functions to make connecting easy in DXCron contexts
171 return DXChannel->get($call);
177 my $lccall = lc $call;
179 my $prog = "$main::root/local/client.pl";
180 $prog = "$main::root/perl/client.pl" if ! -e $prog;
185 # in child, unset warnings, disable debugging and general clean up from us
187 # do "$main::root/perl/Disable_debug.pl";
188 eval "{ package DB; sub DB {} }";
190 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
191 exec $prog, $call, 'connect';
192 dbg('cron', "exec '$prog' failed $!");
194 dbg('cron', "connect to $call started");
196 dbg('cron', "can't fork for $prog $!");
207 # in child, unset warnings, disable debugging and general clean up from us
209 # do "$main::root/perl/Disable_debug.pl";
210 eval "{ package DB; sub DB {} }";
212 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
214 dbg('cron', "exec '$line' failed $!");
216 dbg('cron', "spawn of $line started");
218 dbg('cron', "can't fork for $line $!");