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 !$mtime || $t <= $mtime;
47 # then read in any local ones
52 $mtime = $t if $t <= $mtime;
61 my $fh = new IO::File;
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
168 # is it locally connected?
172 return DXChannel->get($call);
175 # is it remotely connected anywhere (with exact callsign)?
179 return DXCluster->get_exact($call);
182 # is it remotely connected anywhere (ignoring SSIDS)?
186 return DXCluster->get($call);
189 # is it remotely connected anywhere (with exact callsign) and on node?
194 my $ref = DXCluster->get_exact($call);
195 return ($ref && $ref->mynode) ? $ref->mynode->call eq $node : undef;
198 # is it remotely connected anywhere (ignoring SSIDS) and on node?
203 my $ref = DXCluster->get($call);
204 return ($ref && $ref->mynode) ? $ref->mynode->call eq $node : undef;
207 # last time this thing was connected
211 return $main::systime if DXChannel->get($call);
212 my $user = DXUser->get($call);
213 return $user ? $user->lastin : 0;
216 # disconnect a locally connected thing
220 my $dxchan = DXChannel->get($call);
222 if ($dxchan->is_ak1a) {
223 $dxchan->send_now("D", DXProt::pc39($main::mycall, "$main::mycall DXCron"));
225 $dxchan->send_now('D', "");
231 # start a connect process off
235 my $lccall = lc $call;
237 my $prog = "$main::root/local/client.pl";
238 $prog = "$main::root/perl/client.pl" if ! -e $prog;
243 # in child, unset warnings, disable debugging and general clean up from us
245 eval "{ package DB; sub DB {} }";
246 $SIG{HUP} = 'IGNORE';
248 DXChannel::closeall();
249 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
250 exec $prog, $call, 'connect' or dbg('cron', "exec '$prog' failed $!");
252 dbg('cron', "connect to $call started");
254 dbg('cron', "can't fork for $prog $!");
261 # spawn any old job off
269 # in child, unset warnings, disable debugging and general clean up from us
271 eval "{ package DB; sub DB {} }";
272 $SIG{HUP} = 'IGNORE';
274 DXChannel::closeall();
275 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
276 exec "$line" or dbg('cron', "exec '$line' failed $!");
278 dbg('cron', "spawn of $line started");
280 dbg('cron', "can't fork for $line $!");
287 # do an rcmd to another cluster from the crontab
293 # can we see it? Is it a node?
294 my $noderef = DXCluster->get_exact($call);
295 return if !$noderef || !$noderef->pcversion;
298 DXProt::addrcmd($main::mycall, $call, $line);