summaryrefslogtreecommitdiffstats
path: root/usr.sbin/cron
diff options
context:
space:
mode:
authoryar <yar@FreeBSD.org>2004-05-16 19:29:33 +0000
committeryar <yar@FreeBSD.org>2004-05-16 19:29:33 +0000
commitae09cae9394cadcf5605c2b97b33ee0ea580f785 (patch)
treef6891e3a1cac720616add18137adb29bd3f30ecc /usr.sbin/cron
parente37a9f86efaae2be03df45809ad7f396ddf1e29b (diff)
downloadFreeBSD-src-ae09cae9394cadcf5605c2b97b33ee0ea580f785.zip
FreeBSD-src-ae09cae9394cadcf5605c2b97b33ee0ea580f785.tar.gz
Add two new options to cron(8), -J and -j. They allow to specify
the maximum amount of time jitter for root and other users, respectively. Before starting a job, cron(8) will sleep a random number of seconds, from 0 to the amount specified. This can help to smooth down load spikes when a lot of jobs are to start at the beginning of a particular minute (e.g., the first minute of an hour.) PR: bin/66474 Submitted by: Dmitry Morozovsky <marck <@> rinet.ru>
Diffstat (limited to 'usr.sbin/cron')
-rw-r--r--usr.sbin/cron/cron/cron.823
-rw-r--r--usr.sbin/cron/cron/cron.c18
-rw-r--r--usr.sbin/cron/cron/cron.h4
-rw-r--r--usr.sbin/cron/cron/do_command.c7
4 files changed, 50 insertions, 2 deletions
diff --git a/usr.sbin/cron/cron/cron.8 b/usr.sbin/cron/cron/cron.8
index bd5cf35..8ad0622 100644
--- a/usr.sbin/cron/cron/cron.8
+++ b/usr.sbin/cron/cron/cron.8
@@ -25,6 +25,8 @@
.Nd daemon to execute scheduled commands (Vixie Cron)
.Sh SYNOPSIS
.Nm
+.Op Fl j Ar jitter
+.Op Fl J Ar rootjitter
.Op Fl s
.Op Fl o
.Op Fl x Ar debugflag Ns Op , Ns Ar ...
@@ -78,6 +80,27 @@ changes a crontab.
.Pp
Available options:
.Bl -tag -width indent
+.It Fl j Ar jitter
+Enable time jitter.
+Prior to executing commands,
+.Nm
+will sleep a random number of seconds in the range from 0 to
+.Ar jitter .
+This won't affect superuser jobs (see
+.Fl J ) .
+A value for
+.Ar jitter
+must be between 0 and 60 inclusive.
+Default is 0, which effectively disables time jitter.
+.Pp
+This option can help to smooth down system load spikes during
+moments when a lot of jobs are likely to start at once, e.g.,
+at the beginning of the first minute of each hour.
+.It Fl J Ar rootjitter
+Enable time jitter for superuser jobs.
+The same as
+.Fl j
+except that it will affect jobs run by the superuser only.
.It Fl s
Enable special handling of situations when the GMT offset of the local
timezone changes, such as the switches between the standard time and
diff --git a/usr.sbin/cron/cron/cron.c b/usr.sbin/cron/cron/cron.c
index 5131645..17c6894 100644
--- a/usr.sbin/cron/cron/cron.c
+++ b/usr.sbin/cron/cron/cron.c
@@ -51,7 +51,8 @@ static void
usage() {
char **dflags;
- fprintf(stderr, "usage: cron [-s] [-o] [-x debugflag[,...]]\n");
+ fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
+ "[-s] [-o] [-x debugflag[,...]]\n");
fprintf(stderr, "\ndebugflags: ");
for(dflags = DebugFlagNames; *dflags; dflags++) {
@@ -414,9 +415,22 @@ parse_args(argc, argv)
char *argv[];
{
int argch;
+ char *endp;
- while ((argch = getopt(argc, argv, "osx:")) != -1) {
+ while ((argch = getopt(argc, argv, "j:J:osx:")) != -1) {
switch (argch) {
+ case 'j':
+ Jitter = strtoul(optarg, &endp, 10);
+ if (*optarg == '\0' || *endp != '\0' || Jitter > 60)
+ errx(ERROR_EXIT,
+ "bad value for jitter: %s", optarg);
+ break;
+ case 'J':
+ RootJitter = strtoul(optarg, &endp, 10);
+ if (*optarg == '\0' || *endp != '\0' || RootJitter > 60)
+ errx(ERROR_EXIT,
+ "bad value for root jitter: %s", optarg);
+ break;
case 'o':
dst_enabled = 0;
break;
diff --git a/usr.sbin/cron/cron/cron.h b/usr.sbin/cron/cron/cron.h
index 33bdf92..f7b2b7a 100644
--- a/usr.sbin/cron/cron/cron.h
+++ b/usr.sbin/cron/cron/cron.h
@@ -269,6 +269,8 @@ char *DowNames[] = {
char *ProgramName;
int LineNumber;
+unsigned Jitter,
+ RootJitter;
time_t TargetTime;
# if DEBUGGING
@@ -284,6 +286,8 @@ extern char *copyright[],
*DowNames[],
*ProgramName;
extern int LineNumber;
+extern unsigned Jitter,
+ RootJitter;
extern time_t TargetTime;
# if DEBUGGING
extern int DebugFlags;
diff --git a/usr.sbin/cron/cron/do_command.c b/usr.sbin/cron/cron/do_command.c
index 0133fa4..592ca50 100644
--- a/usr.sbin/cron/cron/do_command.c
+++ b/usr.sbin/cron/cron/do_command.c
@@ -166,6 +166,13 @@ child_process(e, u)
Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
getpid()))
+ if (e->uid == ROOT_UID)
+ Jitter = RootJitter;
+ if (Jitter != 0) {
+ srandom(getpid());
+ sleep(random() % Jitter);
+ }
+
/* write a log message. we've waited this long to do it
* because it was not until now that we knew the PID that
* the actual user command shell was going to get and the
OpenPOWER on IntegriCloud