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 # cron initialisation / reading in cronjobs
32 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
35 # first read in the standard one
39 @scrontab = cread($fn);
40 $mtime = $t if !$mtime || $t <= $mtime;
43 # then read in any local ones
47 @lcrontab = cread($localfn);
48 $mtime = $t if $t <= $mtime;
50 @crontab = (@scrontab, @lcrontab);
58 my $fh = new IO::File;
62 dbg("cron: reading $fn\n") if isdbg('cron');
63 open($fh, $fn) or confess("cron: can't open $fn $!");
67 next if /^\s*#/o or /^\s*$/o;
68 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
69 next unless defined $min;
73 $err |= parse($ref, 'min', $min, 0, 60);
74 $err |= parse($ref, 'hour', $hour, 0, 23);
75 $err |= parse($ref, 'mday', $mday, 1, 31);
76 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
77 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
81 dbg("cron: adding $_\n") if isdbg('cron');
83 dbg("cron: error on line $line '$_'\n") if isdbg('cron');
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: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
152 dbg("cron: cmd error $@") if $@ && isdbg('cron');
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 Route::get($call);
181 # is it remotely connected anywhere (ignoring SSIDS)?
185 my $c = Route::get($call);
188 $c = Route::get("$call-$_");
195 # is it remotely connected anywhere (with exact callsign) and on node?
199 my $ncall = uc shift;
200 my $node = Route::Node::get($ncall);
201 return ($node) ? grep $call eq $_, $node->users : undef;
204 # is it remotely connected (ignoring SSIDS) and on node?
208 my $ncall = uc shift;
209 my $node = Route::Node::get($ncall);
212 $present = grep {/^$call/ } $node->users;
217 # last time this thing was connected
221 return $main::systime if DXChannel->get($call);
222 my $user = DXUser->get($call);
223 return $user ? $user->lastin : 0;
226 # disconnect a locally connected thing
230 run_cmd("disconnect $call");
233 # start a connect process off
237 # connecting is now done in one place - Yeah!
238 run_cmd("connect $call");
241 # spawn any old job off
249 # in child, unset warnings, disable debugging and general clean up from us
251 eval "{ package DB; sub DB {} }";
252 DXChannel::closeall();
253 for (@main::listeners) {
256 unless ($main::is_win) {
257 $SIG{HUP} = 'IGNORE';
258 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
261 exec "$line" or dbg("exec '$line' failed $!") if isdbg('cron');
263 dbg("spawn of $line started") if isdbg('cron');
265 dbg("can't fork for $line $!") if isdbg('cron');
272 # do an rcmd to another cluster from the crontab
278 # can we see it? Is it a node?
279 my $noderef = Route::Node::get($call);
280 return unless $noderef && $noderef->version;
283 DXProt::addrcmd($DXProt::me, $call, $line);
289 my @in = DXCommandmode::run_cmd($DXProt::me, $line);
290 dbg("cmd run: $line") if isdbg('cron');
293 dbg("cmd out: $_") if isdbg('cron');