commit: ea42a009ba74ddbcc010f5a8549abe686e2622cc
parent 58cf416e23ac8c5adbccd9d8bb20f6df0de59c56
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 30 Jul 2025 15:15:24 +0200
Add -w option
Diffstat:
2 files changed, 38 insertions(+), 7 deletions(-)
diff --git a/timer.1 b/timer.1
@@ -8,6 +8,7 @@
.Nd run command at a specific interval
.Sh SYNOPSIS
.Nm
+.Op Fl w
.Ar interval
.Ar command
.Op Ar arguments
@@ -31,6 +32,18 @@ If
exits with a non-zero status,
.Nm
exits with the same status, allowing to use service supervision to track failures.
+.Sh OPTIONS
+.Bl -item -width Ds
+.It Fl w
+Wait for
+.Ar interval
+to elapse.
+Otherwise
+.Nm
+immediately runs
+.Ar command
+after starting.
+.El
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
diff --git a/timer.c b/timer.c
@@ -5,15 +5,16 @@
#include "strtodur.h"
#include <errno.h>
-#include <limits.h> // UINT_MAX
-#include <signal.h> // sigaction
-#include <spawn.h> // posix_spawnp
+#include <limits.h> // UINT_MAX
+#include <signal.h> // sigaction
+#include <spawn.h> // posix_spawnp
+#include <stdbool.h>
#include <stdio.h> // fprintf, fputs
#include <stdlib.h> // exit
#include <string.h> // strerror
#include <sys/wait.h> // waitpid
#include <time.h> // timer_create
-#include <unistd.h> // sleep
+#include <unistd.h> // sleep, getopt
extern char **environ;
static char **args;
@@ -47,7 +48,7 @@ sig_chld(int sig)
static void
bad_usage()
{
- fputs("Usage: timer <interval> <command> [arguments...]\n", stderr);
+ fputs("Usage: timer [-w] <interval> <command> [arguments...]\n", stderr);
exit(1);
}
@@ -61,8 +62,23 @@ timer_errx(int err, char *msg)
int
main(int argc, char *argv[])
{
- argc--;
- argv++;
+ bool opt_w = false;
+
+ for(char c = -1; (c = getopt(argc, argv, ":w")) != -1;)
+ {
+ switch(c)
+ {
+ case 'w':
+ opt_w = true;
+ break;
+ default:
+ fprintf(stderr, "timer: error: Unhandled option -%c\n", optopt);
+ return 1;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
if(argc < 2)
{
@@ -146,6 +162,8 @@ main(int argc, char *argv[])
if(sigdelset(&sigmask, SIGTERM) != 0)
timer_errx(1, "Failed adding SIGTERM to sigsuspend sigmask");
+ if(!opt_w) sig_timer(0);
+
while(sigsuspend(&sigmask))
;