Files
sics/tecs/keep_running.c
2001-09-03 14:30:38 +00:00

50 lines
1.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#define MAX_CNT 10
#define MIN_PERIOD 3600
int main(int argc, char *argv[]) {
int cnt, dif;
time_t tim1, tim2;
pid_t pid;
int status, i;
if (argc<2) return 0;
cnt = MAX_CNT;
while (cnt > 0) {
time(&tim1);
pid=vfork();
if (pid == 0) {
execvp(argv[1], argv+1);
perror("execvp");
exit(1);
}
waitpid(pid, &status, 0);
if (status==512) { /* kill */
return 0;
}
if (status==256) { /* restart */
cnt = MAX_CNT;
}
time(&tim2);
dif = (int) difftime(tim2, tim1);
cnt += dif / MIN_PERIOD;
if (cnt > MAX_CNT) cnt = MAX_CNT;
cnt--;
if (dif < MIN_PERIOD) {
printf("Status=%d. Died after %d sec", status, dif);
} else {
printf("Status=%d. Died after %f hours", status, dif/3600.);
}
if (cnt > 0) {
printf(" - %d tries left - retry ...\n", cnt);
}
}
printf(" - too many restarts\n");
return -1;
}