2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
19 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
26 my $fn = "$main::cmd/crontab";
27 my $localfn = "$main::localcmd/crontab";
29 use vars qw($VERSION $BRANCH);
31 main::mkver($VERSION = q$Revision$);
33 # cron initialisation / reading in cronjobs
36 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
39 # first read in the standard one
43 @scrontab = cread($fn);
44 $mtime = $t if !$mtime || $t <= $mtime;
47 # then read in any local ones
51 @lcrontab = cread($localfn);
52 $mtime = $t if $t <= $mtime;
54 @crontab = (@scrontab, @lcrontab);
62 my $fh = new IO::File;
66 dbg("cron: reading $fn\n") if isdbg('cron');
67 open($fh, $fn) or confess("cron: can't open $fn $!");
71 next if /^\s*#/o or /^\s*$/o;
72 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
73 next unless defined $min;
77 $err |= parse($ref, 'min', $min, 0, 60);
78 $err |= parse($ref, 'hour', $hour, 0, 23);
79 $err |= parse($ref, 'mday', $mday, 1, 31);
80 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
81 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
85 dbg("cron: adding $_\n") if isdbg('cron');
87 dbg("cron: error on line $line '$_'\n") if isdbg('cron');
109 # handle comma delimited values
110 my @comma = split /,/o, $val;
112 my @minus = split /-/o;
114 return 1 if $minus[0] < $low || $minus[0] > $high;
115 return 1 if $minus[1] < $low || $minus[1] > $high;
117 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
121 return 1 if $_ < $low || $_ > $high;
125 $ref->{$sort} = \@req;
130 # process the cronjobs
133 my $now = $main::systime;
134 return if $now-$lasttime < 1;
136 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
138 # are we at a minute boundary?
139 if ($min != $lastmin) {
141 # read in any changes if the modification time has changed
144 $mon += 1; # months otherwise go 0-11
146 foreach $cron (@crontab) {
147 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
148 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
149 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
150 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
151 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
154 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
156 dbg("cron: cmd error $@") if $@ && isdbg('cron');
162 # remember when we are now
168 # these are simple stub functions to make connecting easy in DXCron contexts
171 # is it locally connected?
175 return DXChannel::get($call);
178 # is it remotely connected anywhere (with exact callsign)?
182 return Route::get($call);
185 # is it remotely connected anywhere (ignoring SSIDS)?
189 my $c = Route::get($call);
192 $c = Route::get("$call-$_");
199 # is it remotely connected anywhere (with exact callsign) and on node?
203 my $ncall = uc shift;
204 my $node = Route::Node::get($ncall);
205 return ($node) ? grep $call eq $_, $node->users : undef;
208 # is it remotely connected (ignoring SSIDS) and on node?
212 my $ncall = uc shift;
213 my $node = Route::Node::get($ncall);
216 $present = grep {/^$call/ } $node->users;
221 # last time this thing was connected
225 return $main::systime if DXChannel::get($call);
226 my $user = DXUser->get($call);
227 return $user ? $user->lastin : 0;
230 # disconnect a locally connected thing
234 run_cmd("disconnect $call");
237 # start a connect process off
241 # connecting is now done in one place - Yeah!
242 run_cmd("connect $call");
245 # spawn any old job off
253 # in child, unset warnings, disable debugging and general clean up from us
255 eval "{ package DB; sub DB {} }";
256 DXChannel::closeall();
257 for (@main::listeners) {
260 unless ($main::is_win) {
261 $SIG{HUP} = 'IGNORE';
262 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
265 exec "$line" or dbg("exec '$line' failed $!") if isdbg('cron');
267 dbg("spawn of $line started") if isdbg('cron');
269 dbg("can't fork for $line $!") if isdbg('cron');
276 # do an rcmd to another cluster from the crontab
282 # can we see it? Is it a node?
283 my $noderef = Route::Node::get($call);
284 return unless $noderef && $noderef->version;
287 DXProt::addrcmd($main::me, $call, $line);
293 my @in = DXCommandmode::run_cmd($main::me, $line);
294 dbg("cmd run: $line") if isdbg('cron');
297 dbg("cmd out: $_") if isdbg('cron');