OpenBSD Journal

cron(8) now supports random ranges with steps

Contributed by rueda on from the not-a-random-walk dept.

Thanks to the following commit by Todd Miller (millert@), cron(8) now supports random values in a range with a step value (i.e. "<lo>~<hi>/<step>" in crontab(5) entries):

CVSROOT:	/cvs
Module name:	src
Changes by:	millert@cvs.openbsd.org	2023/05/06 17:06:27

Modified files:
	usr.sbin/cron  : crontab.5 entry.c macros.h 

Log message:
Support random offsets when using ranges with a step value in cron.
This extends the random range syntax to support step values.  Instead
of choosing a random number between the high and low values, the
field is treated as a range with a random offset less than the step
value.  This can be used to avoid thundering herd problems where
multiple machines contact a server all at the same time via cron jobs.

The syntax is similar to the existing range/step syntax but uses a
random range.  For example, instead of "0-59/10" in the minutes
field, "0~59/10" can be used to run a command every 10 minutes where
the first command starts at a random offset in the range [0,9].
The high and low numbers are optional, "~/10" can be used instead.

Requested by job@, OK phessler@

(Comments are closed)


Comments
  1. By John McCue (jmcunx) jmcq66@comcast.net on

    Nice, I really like all the changes happening with cron

    If I understand the usage, if I specify "~/20" in the minutes field, will the job start somewhere between minutes 0-19 ?

    Comments
    1. By grey (grey) on http://www.artkiver.com

      That would be my guess? I'm not sure and haven't tested personally yet so others please feel free to correct me. This seems similar to -j (jitter) in FreeBSD's cron (which some have referred to as "Vixie cron" but I think Paul Vixie (cited in the manpage of OpenBSD as well) would probably need to shed more light on the branching of that feature without referring to older source code changes in another project, which I guess I'll leave as "an exercise for the reader" (though honestly now I am curious where/when such feature was introduced).

    2. By Ampie Niemand (Ampie) ampien@gmail.com on

      Hi, I believe ~/20 means every 20 minutes randomly within the hour.

      Comments
      1. By John McCue (jmcunx) jmcq66@comcast.net on

        That makes sense

        so if I have ~/20, it could execute as:

        r1: 17 mins (random time picked)
        r2: Then between 37 -- 57 or probably between 20 -- 40 minutes
        r3: r2 +20 before the end of the hour or somewhere between 40 -- 59

        I will play with it once it hits cron to be sure

Latest Articles

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]