]> git.netwichtig.de Git - user/henk/code/snooze.git/commitdiff
Add option for jitter, fix wording of randdelay
authorAndrew Benson <abenson+void@gmail.com>
Fri, 2 Oct 2020 00:36:42 +0000 (18:36 -0600)
committerLeah Neukirchen <leah@vuxu.org>
Sun, 4 Oct 2020 14:17:04 +0000 (16:17 +0200)
README.md
snooze.1
snooze.c

index efdb463c64a79852df8594008ce4526da38e4771..2a1f364464f59d7d1561f835aa87661618bfc482 100644 (file)
--- 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:
index ee35c7883c3c57d1d7a3bdd062bc29d195161883..67e008e6c631f3b9711c3b69df2b912b73961b3b 100644 (file)
--- 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
index 8bbd93fb366b35437a7fa0aef6cf0610e725074b..06329e389d6afbe858de2ef793b952fa764653e0 100644 (file)
--- 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,