about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--snooze.19
-rw-r--r--snooze.c34
3 files changed, 31 insertions, 17 deletions
diff --git a/README.md b/README.md
index efdb463..2a1f364 100644
--- a/README.md
+++ b/README.md
@@ -71,10 +71,11 @@ Over systemd timers:
 * `-n`: dry-run, print the next 5 times the command would run.
 * `-v`: verbose, print scheduled (and rescheduled) times.
 * `-t`, `-T`: see below timefiles
-* `-R`: add between 0 and RANDDELAY seconds to the scheduled time.
+* `-R`: add between 0 and RANDDELAY seconds to the start of the scheduled time.
+* `-J`: add between 0 and JITTER seconds to scheduled execution time.
 * `-s`: commands are executed even if they are SLACK (default: 60) seconds late.
 
-The durations RANDDELAY and SLACK and TIMEWAIT are parsed as seconds,
+The durations RANDDELAY and JITTER and SLACK and TIMEWAIT are parsed as seconds,
 unless a postfix of `m` for minutes, `h` for hours, or `d` for days is used.
 
 The remaining arguments are patterns for the time fields:
diff --git a/snooze.1 b/snooze.1
index ee35c78..67e008e 100644
--- a/snooze.1
+++ b/snooze.1
@@ -10,6 +10,7 @@
 .Op Fl t Ar timefile
 .Op Fl T Ar timewait
 .Op Fl R Ar randdelay
+.Op Fl J Ar jitter
 .Op Fl s Ar slack
 .Op Fl d Ar day
 .Op Fl m Ar mon
@@ -38,9 +39,13 @@ Verbose: print scheduled (and rescheduled) times.
 See below,
 .Sx TIMEFILES .
 .It Fl R
-Wait randomly up to
+Delay determination of scheduled time randomly up to
 .Ar randdelay
-seconds later than the scheduled time.
+seconds later.
+.It Fl J
+Delay execution randomly up to
+.Ar jitter
+seconds later than scheduled time.
 .It Fl s
 Commands are executed even if they are
 .Ar slack
diff --git a/snooze.c b/snooze.c
index 8bbd93f..06329e3 100644
--- a/snooze.c
+++ b/snooze.c
@@ -28,6 +28,7 @@ static int nflag, vflag;
 
 static int timewait = -1;
 static int randdelay = 0;
+static int jitter = 0;
 static char *timefile;
 
 static sig_atomic_t alarm_rang = 0;
@@ -215,6 +216,14 @@ next_day:
 			goto next_day;
 	}
 
+	if (jitter && !nflag) {
+		long delay;
+		delay = lrand48() % jitter;
+		if (vflag)
+			printf("adding %lds for jitter.\n", delay);
+		t += delay;
+	}
+
 	return t;
 }
 
@@ -246,7 +255,7 @@ main(int argc, char *argv[])
 
 	setvbuf(stdout, 0, _IOLBF, 0);
 
-	while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
+	while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:J:d:m:ns:t:vw:")) != -1)
 		switch (c) {
 		case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
 		case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
@@ -266,8 +275,9 @@ main(int argc, char *argv[])
 		case 'T': timewait = parse_dur(optarg); break;
 		case 't': timefile = optarg; break;
 		case 'R': randdelay = parse_dur(optarg); break;
+		case 'J': jitter = parse_dur(optarg); break;
 		default:
-			fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
+			fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-J jitter] [-s slack]\n"
 			    "  [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
 			    "Timespec: exact: 1,3,5\n"
 			    "          range: 1-7\n"
@@ -296,18 +306,11 @@ main(int argc, char *argv[])
 		}
 	}
 
+	srand48(getpid() ^ start);
+
 	if (randdelay) {
 		long delay;
-#ifdef __linux__
-		long rnd = getauxval(AT_RANDOM);
-		if (rnd > 0)
-			delay = rnd % randdelay;
-		else
-#endif
-		{
-			srand48(getpid() ^ start);
-			delay = lrand48() % randdelay;
-		}
+		delay = lrand48() % randdelay;
 		if (vflag)
 			printf("randomly delaying by %lds.\n", delay);
 		start += delay;
@@ -326,13 +329,18 @@ main(int argc, char *argv[])
 			char weekstr[4];
 			struct tm *tm = localtime(&t);
 			strftime(weekstr, sizeof weekstr, "%a", tm);
-			printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
+			printf("%s %s %2ldd%3ldh%3ldm%3lds ",
 			    isotime(tm),
 			    weekstr,
 			    ((t - now) / (60*60*24)),
 			    ((t - now) / (60*60)) % 24,
 			    ((t - now) / 60) % 60,
 			    (t - now) % 60);
+			if(jitter) {
+				printf("(plus up to %ds for jitter)\n", jitter);
+			} else {
+				printf("\n");
+			}
 			t = find_next(t + 1);
 			if (t < 0) {
 				fprintf(stderr,