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]);
50 signal(SIGHUP, SIG_IGN);
51 signal(SIGINT, SIG_IGN);
52 signal(SIGQUIT, SIG_IGN);
56 fp = fopen(pidfilename, "w");
58 fprintf(fp, "%d\n", child);
65 current_child = fork();
67 signal(SIGTERM, graceful_shutdown);
69 if (current_child < 0) {
74 else if (current_child == 0) {
75 exit(execvp(argv[0], &argv[0]));
79 waitpid(current_child, &status, 0);
82 } while (status != 0);