http://qs321.pair.com?node_id=1222047


in reply to Scheduling a shell script to run every wednesday at 10 AM with perl without cron

I wrote this up years ago, and have a side screen full of little fun console windows counting down to their jobs, with FG/BG colors set based on topic. Using your task scheduler would be better if you want it to be more reliable than entertaining however.

In scripts:
sleepUntil( nextTimeOccurence({time=>'3:15', weekdays=>'MTWRF'}), sub {printf"%s - Next telemetry pull in %s.\r", scalar loc +altime, englishTime(shift)} ); print "\n";
In tools.pm:
sub sleepUntil { my $time = shift; my $sub = shift; my $snooze = shift // 2; while (time < $time) { $sub->($time - time) if ref $sub eq 'CODE'; sleep $snooze; } } sub nextTimeOccurence { my $settings = shift; my $wantedSeconds = 0; if ($settings->{time} =~ /([0-9]+):([0-9]{2})/i) { $wantedSeconds = ($1*60 + $2)*60; } my @skipWeekDays; if ($settings->{weekdays} =~ /[mtwrfau]/i) { @skipWeekDays = qw( u m t w r f a ); #Sunday is day 0 of the w +eek for localtime for (0..$#skipWeekDays) { $skipWeekDays[$_] = 0 if $settings->{weekdays} =~ /$skipWe +ekDays[$_]/i; } } my $now = time; my ($sec, $min, $hour, $wday) = (localtime($now))[0,1,2,6]; my $todaysNow = ($hour*60+$min)*60+$sec; $sec += $min*60 + $hour *3600; #seconds elapsed today my $desiredTime = $now - $sec + $wantedSeconds; #today's time of d +ay if ($todaysNow > $wantedSeconds) #next desired time will occur tom +orrow. { $desiredTime += 24*60*60; $wday++; $wday %= 7; } while ($skipWeekDays[$wday]) { $desiredTime += 24*60*60; $wday++; $wday %= 7; } return $desiredTime; #Process over lunch hour or whatever } sub englishTime { my $time = shift; return "$time second".($time==1?'':'s') if ($time < 90); $time = int($time/60); return "$time minute".($time==1?'':'s') if ($time < 90); $time = int($time/60); return "$time hour".($time==1?'':'s') if ($time < 36); $time = int($time/24); return "$time day".($time==1?'':'s'); }