cronjobs

Ben Wong lists.openmoko.org at wongs.net
Sun May 31 13:01:51 CEST 2009


Sure, I've whipped up an example for you based on the documentation
located here:  http://search.cpan.org/dist/Schedule-Cron/lib/Schedule/Cron.pm

--Ben

#!/usr/bin/perl -w
use Schedule::Cron;

# Dummy subroutine, to make Cron constructor happy.
sub dispatcher {
  print "ID:   ",shift,"\n";
  print "Args: ","@_","\n";
}

my $cron = new Schedule::Cron(\&dispatcher);

# You can print out a date from a single line...
$line='*/5 3 * * 5,6';
print "The crontab entry '$line' will be next executed on ";
print scalar(localtime($cron->get_next_execution_time($line)));
print "\n\n";

# ...or you can iterate through a crontab file.
$cron->load_crontab("/etc/crontab");

my @entries = $cron->list_entries();
foreach $e (@entries) {
    # time in original cron format, "*/5 * * * *"
    print $$e{'time'} . "\t";

    my $nexttime=$cron->get_next_execution_time($$e{'time'});

    # time in seconds since the epoch
    print $nexttime . "\t";

    # time in human readable format
    print scalar(localtime($nexttime)) . "\t";

    # the rest of the line from the crontab file
    @command = $$e{'args'};
    print $command[0][0];
    print "\n";
}

# END OF SCRIPT



On Sat, May 30, 2009 at 6:29 PM, Ed Kapitein <ed at kapitein.org> wrote:
> Ben Wong wrote:
>> Did  you try  $cron->get_next_execution_time($cron_entry,[$ref_time]) ?
>>
>> --Ben
>>
> No, i had no idea i could !
> But now that i try it i get:
> Can't call method "get_next_execution_time" on an undefined value at
> ./ff.pl line 11.
>
> Can you make an example perl script? a real simple one?
>
> Thanks !
>
> Kind regards,
>
> Ed
>
>
>> On Sat, May 30, 2009 at 4:21 PM, Ed Kapitein <ed at kapitein.org> wrote:
>>
>>> Alexey Feldgendler wrote:
>>>
>>>> On Fri, 29 May 2009 15:49:27 +0200, Ed Kapitein <ed at kapitein.org> wrote:
>>>>
>>>>
>>>>
>>>>
>>>>> i need a script/program that will read a crontab line
>>>>> ( * * * * * some_action) and output the date and time the job will run.
>>>>>
>>>>> Does anyone know of such a tool?
>>>>> This way i can un-suspend the FR a few seconds prior to the start of
>>>>> that cron job.
>>>>>
>>>>> I already made a script that sort of works, but it is limmited at best
>>>>> and i hate to reinvent the wheel again.
>>>>>
>>>>>
>>>> You might want to look at this:
>>>> http://search.cpan.org/dist/Schedule-Cron
>>>>
>>>>
>>> Hi Alexey and all the others
>>>
>>> I did take a look at perl, but i am a real perl noob.
>>>
>>> This is what i have so far:
>>>
>>> #!/usr/bin/perl
>>> use Config::Crontab
>>> $event = new Config::Crontab::Event( -data => '*/5 3 * * 5,6 /bin/blech' );
>>> print $event->minute . "\n";
>>> print $event->hour . "\n";
>>> print $event->dom . "\n";
>>> print $event->datetime . "\n";
>>> print $event->command . "\n";
>>> print $event->dump . "\n";
>>>
>>> And if i run that i get:
>>>
>>> */5
>>> 3
>>> *
>>> */5 3 * * 5,6
>>> /bin/blech
>>> */5 3 * * 5,6 /bin/blech
>>>
>>> Perhaps i am missing the obvious here.
>>>
>>> Is there someone out there who can make a script that can read a
>>> crontab line and give the time and date  of the next time it will be
>>> executed ?
>>> output in the form:
>>> secs_since_epoch,yyyy-mm-dd,hh:mm,command would be *great*
>>>
>>> Many thanks in advance,
>>>
>>> Ed
>>>
>>>
>>>
>>> _______________________________________________
>>> Openmoko community mailing list
>>> community at lists.openmoko.org
>>> http://lists.openmoko.org/mailman/listinfo/community
>>>
>>>
>>
>> _______________________________________________
>> Openmoko community mailing list
>> community at lists.openmoko.org
>> http://lists.openmoko.org/mailman/listinfo/community
>>
>
>
> _______________________________________________
> Openmoko community mailing list
> community at lists.openmoko.org
> http://lists.openmoko.org/mailman/listinfo/community
>




More information about the community mailing list