Add respawn
This commit is contained in:
parent
e8b61e9b7e
commit
c4ff95798c
2
Makefile
2
Makefile
|
@ -40,6 +40,7 @@ SRC = \
|
||||||
pidof.c \
|
pidof.c \
|
||||||
pivot_root.c \
|
pivot_root.c \
|
||||||
ps.c \
|
ps.c \
|
||||||
|
respawn.c \
|
||||||
rmmod.c \
|
rmmod.c \
|
||||||
stat.c \
|
stat.c \
|
||||||
su.c \
|
su.c \
|
||||||
|
@ -67,6 +68,7 @@ MAN1 = \
|
||||||
pagesize.1 \
|
pagesize.1 \
|
||||||
pidof.1 \
|
pidof.1 \
|
||||||
ps.1 \
|
ps.1 \
|
||||||
|
respawn.1 \
|
||||||
stat.1 \
|
stat.1 \
|
||||||
su.1 \
|
su.1 \
|
||||||
truncate.1 \
|
truncate.1 \
|
||||||
|
|
12
respawn.1
Normal file
12
respawn.1
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
.TH RESPAWN 1 ubase-VERSION
|
||||||
|
.SH NAME
|
||||||
|
\fBrespawn\fR - Spawn the given command repeatedly
|
||||||
|
.SH SYNOPSIS
|
||||||
|
\fBrespawn\fR [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR]
|
||||||
|
.SH DESCRIPTION
|
||||||
|
\fBrespawn\fR spawns the given \fIcmd\fR in a new session
|
||||||
|
repeatedly.
|
||||||
|
.SH OPTIONS
|
||||||
|
.TP
|
||||||
|
\fB-d\fR
|
||||||
|
Set the delay between invocations of \fIcmd\fR. It defaults to 0.
|
54
respawn.c
Normal file
54
respawn.c
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(void)
|
||||||
|
{
|
||||||
|
eprintf("usage: respawn [-d N] cmd [args...]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
int savederrno;
|
||||||
|
unsigned int delay = 0;
|
||||||
|
|
||||||
|
ARGBEGIN {
|
||||||
|
case 'd':
|
||||||
|
delay = estrtol(EARGF(usage()), 0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
} ARGEND;
|
||||||
|
|
||||||
|
if(argc < 1)
|
||||||
|
usage();
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
pid = fork();
|
||||||
|
if (pid < 0)
|
||||||
|
eprintf("fork:");
|
||||||
|
switch (pid) {
|
||||||
|
case 0:
|
||||||
|
if (setsid() < 0)
|
||||||
|
eprintf("setsid:");
|
||||||
|
execvp(argv[0], argv);
|
||||||
|
savederrno = errno;
|
||||||
|
weprintf("execvp %s:", argv[0]);
|
||||||
|
_exit(savederrno == ENOENT ? 127 : 126);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
waitpid(pid, NULL, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sleep(delay);
|
||||||
|
}
|
||||||
|
/* not reachable */
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user