commit c4ff95798cd66d7c36789e58db83fc51d46442fc
Author: sin <[email protected]>
Date:   Thu Apr 17 14:00:36 2014 +0100

    Add respawn

diff --git a/Makefile b/Makefile
index 05896d6..dd2f1cd 100644
--- a/Makefile
+++ b/Makefile
@@ -40,6 +40,7 @@ SRC = \
        pidof.c             \
        pivot_root.c        \
        ps.c                \
+       respawn.c           \
        rmmod.c             \
        stat.c              \
        su.c                \
@@ -67,6 +68,7 @@ MAN1 = \
        pagesize.1          \
        pidof.1             \
        ps.1                \
+       respawn.1           \
        stat.1              \
        su.1                \
        truncate.1          \
diff --git a/respawn.1 b/respawn.1
new file mode 100644
index 0000000..f9d68a1
--- /dev/null
+++ b/respawn.1
@@ -0,0 +1,12 @@
+.TH RESPAWN 1 ubase-VERSION
+.SH NAME
+BrespawnR - Spawn the given command repeatedly
+.SH SYNOPSIS
+BrespawnR [B-dI NR] IcmdR [Iargs...R]
+.SH DESCRIPTION
+BrespawnR spawns the given IcmdR in a new session
+repeatedly.
+.SH OPTIONS
+.TP
+B-dR
+Set the delay between invocations of IcmdR.  It defaults to 0.
diff --git a/respawn.c b/respawn.c
new file mode 100644
index 0000000..c2aeea0
--- /dev/null
+++ b/respawn.c
@@ -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...]
");
+}
+
+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;
+}


Reply via email to