Cron::Sequencer
my $crontab = Cron::Sequencer->new("/path/to/crontab"); print encode_json([$crontab->sequence($start, $end)]);
This class can take one or more crontabs and show the sequence of commands that they would run for the time interval requested.
new takes a list of arguments each representing a crontab file, passes each in turn to Cron::Sequence::Parser->new, and then combines the parsed files into a single set of crontab events.
new
Cron::Sequence::Parser->new
See "new" in Cron::Sequencer::Parser for the various formats to specify a crontab file or its contents.
Generates the sequence of commands that the crontab(s) would run for the specific time interval. from and to are in epoch seconds, from is inclusive, end exclusive.
Hence for this input:
30 12 * * * lunch! 30 12 * * 5 POETS!
Calling $crontab->sequence(45000, 131400) generates this output:
$crontab->sequence(45000, 131400)
[ [ { command => "lunch!", env => undef, file => "reminder", lineno => 1, time => 45000, unset => undef, when => "30 12 * * *", }, ], ]
where the event(s) at 131400 are not reported, because the end is exclusive. Whereas $crontab->sequence(45000, 131401) shows:
131400
$crontab->sequence(45000, 131401)
[ [ { command => "lunch!", env => undef, file => "reminder", lineno => 1, time => 45000, unset => undef, when => "30 12 * * *", }, ], [ { command => "lunch!", env => undef, file => "reminder", lineno => 1, time => 131400, unset => undef, when => "30 12 * * *", }, { command => "POETS!", env => undef, file => "reminder", lineno => 2, time => 131400, unset => undef, when => "30 12 * * 5", }, ], ]
The output is structured as a list of lists, with events that fire at the same time grouped as lists. This makes it easier to find cases where different crontab lines trigger at the same time.
This module uses Algorithm::Cron to implement the cron scheduling, but has its own crontab file parser. There are many other modules on CPAN:
Parses, edits and outputs crontab files
Outputs crontab files
Parse a cron entry and check against current time
Generate recurrence sets from crontab lines and files
Cron-style datetime patterns and algorithm (for Mojolicious)
Parses crontab files
A Pegex crontab Parser
"Class for working with Cron" (for qbit)
Expands crontab integer lists
cron-like scheduler for Perl subroutines
take a line from a crontab and find out when events will occur
Parser for crontab time specifications
These modules fall into roughly three groups
Abstract crontab file parsing and manipulation
crontab
Parsing individule command time specification strings
Scheduling events in real time
None of the "schedulers" are easy to adapt to show events (rather than running them) and to do so for arbitrary time intervals. The parsers effectively provide an "abstract syntax tree" for the crontab, but by design don't handle "compiling" this into a sequence of "this command, with these environment variable definitions in scope". The parser/compiler in this module is 70 lines of code, including comments, and handles various corner cases and quirks of the vixie crontab C parser code. Interfacing to one of AST parser modules and implementing a "compiler" on it would likely be more code than this.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. If you would like to contribute documentation, features, bug fixes, or anything else then please raise an issue / pull request:
https://github.com/Humanstate/cron-sequencer
Nicholas Clark - nick@ccl4.org
nick@ccl4.org
To install Cron::Sequencer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cron::Sequencer
CPAN shell
perl -MCPAN -e shell install Cron::Sequencer
For more information on module installation, please visit the detailed CPAN module installation guide.