2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
19 use vars qw{@crontab $mtime $lasttime $lastmin};
27 my $fn = "$main::cmd/crontab";
28 my $localfn = "$main::localcmd/crontab";
30 # cron initialisation / reading in cronjobs
33 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
38 # first read in the standard one
43 $mtime = $t if !$mtime || $t <= $mtime;
46 # then read in any local ones
51 $mtime = $t if $t <= $mtime;
60 my $fh = new IO::File;
63 dbg('cron', "cron: reading $fn\n");
64 open($fh, $fn) or confess("cron: can't open $fn $!");
68 next if /^\s*#/o or /^\s*$/o;
69 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
74 $err |= parse($ref, 'min', $min, 0, 60);
75 $err |= parse($ref, 'hour', $hour, 0, 23);
76 $err |= parse($ref, 'mday', $mday, 1, 31);
77 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
78 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
82 dbg('cron', "cron: adding $_\n");
84 dbg('cron', "cron: error on line $line '$_'\n");
105 # handle comma delimited values
106 my @comma = split /,/o, $val;
108 my @minus = split /-/o;
110 return 1 if $minus[0] < $low || $minus[0] > $high;
111 return 1 if $minus[1] < $low || $minus[1] > $high;
113 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
117 return 1 if $_ < $low || $_ > $high;
121 $ref->{$sort} = \@req;
126 # process the cronjobs
129 my $now = $main::systime;
130 return if $now-$lasttime < 1;
132 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
134 # are we at a minute boundary?
135 if ($min != $lastmin) {
137 # read in any changes if the modification time has changed
140 $mon += 1; # months otherwise go 0-11
142 foreach $cron (@crontab) {
143 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
144 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
145 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
146 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
147 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
150 dbg('cron', "cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'");
152 dbg('cron', "cron: cmd error $@") if $@;
158 # remember when we are now
164 # these are simple stub functions to make connecting easy in DXCron contexts
167 # is it locally connected?
171 return DXChannel->get($call);
174 # is it remotely connected anywhere (with exact callsign)?
178 return DXCluster->get_exact($call);
181 # is it remotely connected anywhere (ignoring SSIDS)?
185 return DXCluster->get($call);
188 # is it remotely connected anywhere (with exact callsign) and on node?
193 my $ref = DXCluster->get_exact($call);
194 return ($ref && $ref->mynode) ? $ref->mynode->call eq $node : undef;
197 # is it remotely connected anywhere (ignoring SSIDS) and on node?
202 my $ref = DXCluster->get($call);
203 return ($ref && $ref->mynode) ? $ref->mynode->call eq $node : undef;
206 # last time this thing was connected
210 return $main::systime if DXChannel->get($call);
211 my $user = DXUser->get($call);
212 return $user ? $user->lastin : 0;
215 # disconnect a locally connected thing
219 my $dxchan = DXChannel->get($call);
221 if ($dxchan->is_ak1a) {
222 $dxchan->send_now("D", DXProt::pc39($main::mycall, "$main::mycall DXCron"));
224 $dxchan->send_now('D', "");
230 # start a connect process off
234 my $lccall = lc $call;
236 if (grep {$_->{call} eq $call} @main::outstanding_connects) {
237 dbg('cron', "Connect not started, outstanding connect to $call");
241 my $prog = "$main::root/local/client.pl";
242 $prog = "$main::root/perl/client.pl" if ! -e $prog;
247 # in child, unset warnings, disable debugging and general clean up from us
249 eval "{ package DB; sub DB {} }";
250 $SIG{HUP} = 'IGNORE';
252 DXChannel::closeall();
253 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
254 exec $prog, $call, 'connect' or dbg('cron', "exec '$prog' failed $!");
256 dbg('cron', "connect to $call started");
258 dbg('cron', "can't fork for $prog $!");
265 # spawn any old job off
273 # in child, unset warnings, disable debugging and general clean up from us
275 eval "{ package DB; sub DB {} }";
276 $SIG{HUP} = 'IGNORE';
278 DXChannel::closeall();
279 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
280 exec "$line" or dbg('cron', "exec '$line' failed $!");
282 dbg('cron', "spawn of $line started");
284 dbg('cron', "can't fork for $line $!");
291 # do an rcmd to another cluster from the crontab
297 # can we see it? Is it a node?
298 my $noderef = DXCluster->get_exact($call);
299 return if !$noderef || !$noderef->pcversion;
302 DXProt::addrcmd($main::mycall, $call, $line);