Add catsit-timer utility

This commit is contained in:
C. McEnroe 2021-02-25 19:45:56 -05:00
parent fd25c666d5
commit 69c1b1b2ac
5 changed files with 135 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
*.o
catsit
catsit-timer
catsit-watch
catsit.conf
catsitd

View File

@ -16,7 +16,7 @@ RC_SCRIPT = ${UNAME}/catsitd
-include config.mk
BINS = catsit-watch
BINS = catsit-timer catsit-watch
SBINS = catsit catsitd
MAN1 = ${BINS:=.1}
MAN5 = catsit.conf.5

62
catsit-timer.1 Normal file
View File

@ -0,0 +1,62 @@
.Dd February 25, 2021
.Dt CATSIT-TIMER 1
.Os
.
.Sh NAME
.Nm catsit-timer
.Nd run command at interval
.
.Sh SYNOPSIS
.Nm
.Ar interval
.Ar command ...
.
.Sh DESCRIPTION
The
.Nm
utility runs a command
at an interval.
The command is run once immediately,
then again every
.Ar interval
after waiting for the command to exit
each time.
The
.Nm
utility is not appropriate
for scheduling commands
to run at specific times.
.
.Pp
The format of the
.Ar interval
specifier is a series of integers
followed by units:
.Cm s
for seconds,
.Cm m
for minutes
and
.Cm h
for hours.
An integer with no unit
is assumed to be in seconds.
The
.Ar interval
is the sum of each
integer-unit pair.
For example,
.Cm 1m30s
is equivalent to
.Cm 90s .
.
.Sh EXIT STATUS
If the command exits non-zero,
.Nm
exits with the same status.
.
.Sh SEE ALSO
.Xr catsitd 8
.
.Sh AUTHORS
.An June Bug Aq Mt june@causal.agency

70
catsit-timer.c Normal file
View File

@ -0,0 +1,70 @@
/* Copyright (C) 2021 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <unistd.h>
static void run(char *argv[]) {
pid_t pid = fork();
if (pid < 0) err(EX_OSERR, "fork");
if (!pid) {
execvp(argv[0], argv);
err(126, "%s", argv[0]);
}
int status;
pid = wait(&status);
if (pid < 0) err(EX_OSERR, "wait");
if (WIFEXITED(status)) {
status = WEXITSTATUS(status);
if (status) exit(status);
} else {
exit(status);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) errx(EX_USAGE, "interval required");
if (argc < 3) errx(EX_USAGE, "command required");
unsigned interval = 0;
char *spec = argv[1];
while (*spec) {
unsigned num = strtoul(spec, &spec, 10);
switch (*spec) {
break; case '\0': interval += num;
break; case 's': spec++; interval += num;
break; case 'm': spec++; interval += 60 * num;
break; case 'h': spec++; interval += 60 * 60 * num;
break; default: errx(EX_USAGE, "invalid interval unit %c", *spec);
}
}
if (!interval) errx(EX_USAGE, "invalid zero interval");
#ifdef __OpenBSD__
int error = pledge("stdio proc exec", NULL);
if (error) err(EX_OSERR, "pledge");
#endif
for (;;) {
run(&argv[2]);
sleep(interval);
}
}

View File

@ -80,6 +80,7 @@ pounce/tilde pounce ${0#*/}.conf
.Ed
.
.Sh SEE ALSO
.Xr catsit-timer 1 ,
.Xr catsit-watch 1 ,
.Xr catsitd 8
.