Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron

by SuicideJunkie (Vicar)
on Sep 10, 2018 at 14:06 UTC ( #1222047=note: print w/replies, xml ) Need Help??

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";
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'); }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222047]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2021-10-17 12:10 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (71 votes). Check out past polls.