There appears to be a strong preference for a syntax like "~/10"
instead of "*/~10".  The following diff implements that instead and
also supports random ranges like "1~58/10".  When a high and/or low
value is used with a random range, the initial offset is a random
value less than eiter the step value or the difference of the high
and low values (whichever is smaller).

 - todd

Index: usr.sbin/cron/crontab.5
===================================================================
RCS file: /cvs/src/usr.sbin/cron/crontab.5,v
retrieving revision 1.41
diff -u -p -u -r1.41 crontab.5
--- usr.sbin/cron/crontab.5     18 Apr 2020 17:11:40 -0000      1.41
+++ usr.sbin/cron/crontab.5     6 May 2023 02:19:40 -0000
@@ -157,8 +157,7 @@ If either (or both) of the numbers on ei
 .Ql ~
 are omitted, the appropriate limit (low or high) for the field will be used.
 .Pp
-Step values can be used in conjunction with ranges (but not random ranges
-which represent a single number).
+Step values can be used in conjunction with ranges.
 Following a range with
 .No / Ns Ar number
 specifies skips of
@@ -173,6 +172,18 @@ Steps are also permitted after an asteri
 .Dq every two hours ,
 just use
 .Dq */2 .
+A step value after a random range will execute the command at a random
+offset less than the step size.
+For example, to avoid a thundering herd at the top and bottom of the hour,
+.Dq 0~59/30
+.Po
+or simply
+.Dq ~/30
+.Pc
+can be used in the
+.Ar minute
+field to specify that command execution happen twice an hour at
+consistent intervals.
 .Pp
 An asterisk
 .Pq Ql *
Index: usr.sbin/cron/entry.c
===================================================================
RCS file: /cvs/src/usr.sbin/cron/entry.c,v
retrieving revision 1.53
diff -u -p -u -r1.53 entry.c
--- usr.sbin/cron/entry.c       21 May 2022 01:21:29 -0000      1.53
+++ usr.sbin/cron/entry.c       5 May 2023 21:39:30 -0000
@@ -456,10 +456,11 @@ get_range(bitstr_t *bits, int low, int h
        /* range = number | number* "~" number* | number "-" number ["/" number]
         */
 
-       int i, num1, num2, num3;
+       int i, num1, num2, num3, rndstep;
 
        num1 = low;
        num2 = high;
+       rndstep = 0;
 
        if (ch == '*') {
                /* '*' means [low, high] but can still be modified by /step
@@ -497,7 +498,7 @@ get_range(bitstr_t *bits, int low, int h
 
                        /* get the (optional) number following the tilde
                         */
-                       ch = get_number(&num2, low, names, ch, file, ", \t\n");
+                       ch = get_number(&num2, low, names, ch, file, "/, \t\n");
                        if (ch == EOF)
                                ch = get_char(file);
                        if (ch == EOF || num1 > num2) {
@@ -505,6 +506,13 @@ get_range(bitstr_t *bits, int low, int h
                                return (EOF);
                        }
 
+                       if (ch == '/') {
+                               /* randomize the step value instead of num1
+                                */
+                               rndstep = 1;
+                               break;
+                       }
+
                        /* get a random number in the interval [num1, num2]
                         */
                        num3 = num1;
@@ -538,6 +546,13 @@ get_range(bitstr_t *bits, int low, int h
                ch = get_number(&num3, 0, NULL, ch, file, ", \t\n");
                if (ch == EOF || num3 == 0)
                        return (EOF);
+               if (rndstep) {
+                       /*
+                        * use a random offset smaller than the step size
+                        * and the difference between high and low values.
+                        */
+                       num1 += arc4random_uniform(MINIMUM(num3, num2 - num1));
+               }
        } else {
                /* no step.  default==1.
                 */
Index: usr.sbin/cron/macros.h
===================================================================
RCS file: /cvs/src/usr.sbin/cron/macros.h,v
retrieving revision 1.15
diff -u -p -u -r1.15 macros.h
--- usr.sbin/cron/macros.h      12 Nov 2015 21:12:05 -0000      1.15
+++ usr.sbin/cron/macros.h      5 May 2023 21:38:19 -0000
@@ -29,6 +29,8 @@
 #define        MAX_TEMPSTR     100     /* obvious */
 #define        MAX_UNAME       (_PW_NAME_LEN+1)        /* max length of 
username, should be overkill */
 
+#define        MINIMUM(a, b)   (((a) < (b)) ? (a) : (b))
+
 #define        Skip_Blanks(c, f) \
                        while (c == '\t' || c == ' ') \
                                c = get_char(f);

Reply via email to