Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created May 4, 2019 18:29
Show Gist options
  • Select an option

  • Save simon-engledew/06384dd29377693b34a8c35eb1fc4e03 to your computer and use it in GitHub Desktop.

Select an option

Save simon-engledew/06384dd29377693b34a8c35eb1fc4e03 to your computer and use it in GitHub Desktop.
Run a command, restarting it on SIGHUP.
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
volatile sig_atomic_t pid = 0;
static void restart(int sig)
{
kill(pid, SIGKILL);
}
void run(char** argv)
{
pid = fork();
if (pid < 0) {
exit(1);
}
else if (pid == 0) {
if (execvp(argv[0], argv) < 0) {
kill(getppid(), SIGINT);
}
}
else {
int status;
while (wait(&status) != pid);
}
}
int main(int argc, char **argv)
{
signal(SIGHUP, restart);
int status;
for (;;) {
run(&argv[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment