1Algorithm::Cron(3) User Contributed Perl Documentation Algorithm::Cron(3)
2
3
4
6 "Algorithm::Cron" - abstract implementation of the cron(8) scheduling
7 algorithm
8
10 use Algorithm::Cron;
11
12 my $cron = Algorithm::Cron->new(
13 base => 'local',
14 crontab => "*/10 9-17 * * *",
15 );
16
17 my $time = time;
18 while(1) {
19 $time = $cron->next_time( $time );
20
21 sleep( time - $time );
22
23 print "Do something\n";
24 }
25
27 Objects in this class implement a time scheduling algorithm such as
28 used by cron(8). Objects are stateless once constructed, and represent
29 a single schedule as defined by a crontab(5) entry. The object
30 implements a method "next_time" which returns an epoch timestamp value
31 to indicate the next time included in the crontab schedule.
32
33 Crontabs
34 The schedule is provided as a set of acceptable values for each field
35 of the broken-down time (as returned by "localtime" or "gmtime"),
36 either in a single string called "crontab" or by a set of named
37 strings, each taking the name of a crontab(5) field.
38
39 my $cron = Algorithm::Cron->new(
40 base => 'local',
41 crontab => '0 9 * * mon-fri',
42 );
43
44 my $cron = Algorithm::Cron->new(
45 base => 'local',
46 min => 0,
47 hour => 9,
48 wday => "mon-fri",
49 );
50
51 A "crontab" field containing a single asterisk ("*"), or a missing
52 named field, indicates that any value here is included in the scheduled
53 times. To restrict the schedule, a value or set of values can be
54 provided. This should consist of one or more comma-separated numbers or
55 ranges, where a range is given as the start and end points, both
56 inclusive.
57
58 hour => "3-6"
59 hour => "3,4,5,6"
60
61 Ranges can also be prefixed by a value to give the increment for values
62 in that range.
63
64 min => "*/10"
65 min => "0,10,20,30,40,50"
66
67 The "mon" and "wday" fields also allow symbolic month or weekday names
68 in place of numeric values. These names are always in the C locale,
69 regardless of the system's locale settings.
70
71 mon => "mar-sep"
72
73 wday => "mon,wed,fri"
74
75 Specifying "sun" as the end of a "wday" range, or giving the numeric
76 value of 7 is also supported.
77
78 wday => "fri-sun"
79 wday => "5-7"
80 # Both equivalent to: wday => "0,5,6"
81
82 As per cron(8) behaviour, this algorithm looks for a match of the
83 "min", "hour" and "mon" fields, and at least one of the "mday" or
84 "mday" fields. If both "mday" and "wday" are specified, a match of
85 either will be sufficient.
86
87 As an extension, seconds may be provided either by passing six space-
88 separated fields in the "crontab" string, or as an additional "sec"
89 field. If not provided it will default to 0. If six fields are
90 provided, the first gives the seconds.
91
92 Time Base
93 "Algorithm::Cron" supports using either UTC or the local timezone when
94 comparing against the given schedule.
95
97 $cron = Algorithm::Cron->new( %args )
98 Constructs a new "Algorithm::Cron" object representing the given
99 schedule relative to the given time base. Takes the following named
100 arguments:
101
102 base => STRING
103 Gives the time base used for scheduling. Either "utc" or
104 "local".
105
106 crontab => STRING
107 Gives the crontab schedule in 5 or 6 space-separated fields.
108
109 sec => STRING, min => STRING, ... mon => STRING
110 Optional. Gives the schedule in a set of individual fields, if
111 the "crontab" field is not specified.
112
114 @seconds = $cron->sec
115 @minutes = $cron->min
116 @hours = $cron->hour
117 @mdays = $cron->mday
118 @months = $cron->mon
119 @wdays = $cron->wday
120 Accessors that return a list of the accepted values for each scheduling
121 field. These are returned in a plain list of numbers, regardless of
122 the form they were specified to the constructor.
123
124 Also note that the list of valid months will be 0-based (in the range 0
125 to 11) rather than 1-based, to match the values used by "localtime",
126 "gmtime", "mktime" and "timegm".
127
128 $time = $cron->next_time( $start_time )
129 Returns the next scheduled time, as an epoch timestamp, after the given
130 timestamp. This is a stateless operation; it does not change any state
131 stored by the $cron object.
132
134 Paul Evans <leonerd@leonerd.org.uk>
135
136
137
138perl v5.36.0 2022-07-22 Algorithm::Cron(3)