4 * This is just a quick little hack to start a program in the background,
5 * and automatically restart it if it exits with a nonzero exit status.
12 #include <sys/types.h>
17 char *pidfilename = NULL;
18 pid_t current_child = 0;
20 void graceful_shutdown(int signum) {
21 kill(current_child, signum);
22 if (pidfilename != NULL) {
29 int main(int argc, char **argv)
38 pidfilename = argv[0];
42 if (access(argv[0], X_OK)) {
43 fprintf(stderr, "%s: cannot execute\n", argv[0]);
49 signal(SIGHUP, SIG_IGN);
50 signal(SIGINT, SIG_IGN);
51 signal(SIGQUIT, SIG_IGN);
55 fp = fopen(pidfilename, "w");
57 fprintf(fp, "%d\n", child);
64 current_child = fork();
66 signal(SIGTERM, graceful_shutdown);
68 if (current_child < 0) {
73 else if (current_child == 0) {
74 exit(execvp(argv[0], &argv[0]));
78 waitpid(current_child, &status, 0);
81 } while (status != 0);