Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by kshitij (Sexton)
on Sep 09, 2018 at 17:30 UTC ( [id://1221986]=perlquestion: print w/replies, xml ) Need Help??

kshitij has asked for the wisdom of the Perl Monks concerning the following question:

Hi All ,

Could you help me out with one perl script to schedule a shell script to be executed automatically every Wednesday at 10 AM per week. I dont want to use crontab or cronjob here.

Thanks Kshitij
  • Comment on Scheduling a shell script to run every wednesday at 10 AM with perl without cron

Replies are listed 'Best First'.
Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by afoken (Chancellor) on Sep 09, 2018 at 18:10 UTC
    I dont want to use crontab or cronjob here.

    Why? That's EXACTLY what cron is for.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      hi Alexander ,

      I dont have permission to use the cron job

      Thanks Kshitij

        What does your script do? I presume it doesn't do anything root-level, or you wouldn't have permission to use those things either.

        Every Unix user has their own crontab; you don't need to use system cron (which would in fact require root level permissions).

        Personally, if this is for work and you absolutely require permissions to run cron as root, I'd be speaking to management as to why they require the extra work on your behalf when something built-in and wholly reliable already exists. Get the permissions you need, or perhaps rethink your ambitions. What is it you're trying to do, anyway?

        If you don't have permission to run a cron job then you probably also don't have permission to run effectively the same thing from perl - seeing as how there's no real difference. If your problem is an adminstrative one then you'll need to find an adminstrative solution.

        Is there some reason you can't ask your systems administrator to review the script abs setup the cron job?

Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by LanX (Saint) on Sep 09, 2018 at 17:54 UTC
    Well you could set alarm , but this would mean you have to make sure that your Perl script is continuously running (in exactly one instance to avoid duplication)

    update

    probably of interest: Schedule::Cron

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

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

    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'); }
Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by zentara (Archbishop) on Sep 09, 2018 at 19:16 UTC
    Yes you could do it easily, and actually could be used as a computer hacking system. You set up a Perl script to run, preferrably as a Daemon, which wakes up once an hour, to check the time, and syncs the clocks. As the set runtime gets close the Daemon would wake up more and more frequently, checking the time, until at the exact time, considering program delay settings, it starts and runs the program.

    A sleeping Daemon listening on a socket takes very little CPU. Look at Proc::Daemon


    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by trippledubs (Deacon) on Sep 10, 2018 at 14:46 UTC
    You always need to sleep this amount before running, not adjusted for local time zone
    #!/usr/bin/env perl # snooze.pl use strict; use warnings; use Time::Local; my $min = 60; my $hour = 60 * $min; my $day = 24 * $hour; my $week = 7 * $day; # First W 10 AM # Epoch is Thursday 00:00:00 Jan 1, 1970 my $w = timegm(gmtime(6 *$day + 10*$hour)); # Upcoming Wednesday 10AM $w += $week until ($w > time); my $diff = $w - time; print $diff
    At the prompt
    server$ (while :; do sleep $(snooze.pl); doJob.pl; done &)
Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by talexb (Chancellor) on Sep 11, 2018 at 20:01 UTC

    No crontab? How about using the at command instead? Documentation here.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Scheduling a shell script to run every wednesday at 10 AM with perl without cron
by Anonymous Monk on Sep 10, 2018 at 15:45 UTC
    You could use Algorithm::Cron combined with either a while(1) loop or an IO::Async::Timer::Periodic to check if the cron is ready to fire. But it is always better to use cron for this sort of thing when you can.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1221986]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found