commit 0e20e9f2b236e5497b64ac4ddf49aa490d4a3760
parent 38137b33f3cc2ef295488af11ec0b3b66e2eae30
Author: Ari Malinen <ari.malinen@gmail.com>
Date: Thu, 12 Jun 2014 21:12:24 +0300
switched daemon() and system() to fork(), improved timing
Diffstat:
2 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/README b/README
@@ -3,8 +3,6 @@ dcron - simple cron daemon
features:
Single daemon, config and initscript.
Logs to stdout and syslog.
-Waits for job to finish and checks return value.
-You can disable this by appending & to command.
usage:
usage: dcron [options]
@@ -13,6 +11,6 @@ usage: dcron [options]
-f <file> config file
config:
-# min,hour,mday,mon,wday,command
-10 6 * * * exec ionice -n 2 updatedb
-20 6 * * * exec nice -n 10 mandb --quiet
+# tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command
+10 6 * * * exec updatedb
+20 6 * * * exec mandb --quiet
diff --git a/dcron.c b/dcron.c
@@ -1,4 +1,3 @@
-#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -20,10 +19,19 @@ int main(int argc, char *argv[]) {
for (i = 1; i < argc; i++) {
if (!strcmp("-d", argv[i])) {
- if (daemon(1, 0) != 0) {
- fprintf(stderr, "error: failed to daemonize\n");
- syslog(LOG_NOTICE, "error: failed to daemonize");
- return 1;
+ switch (fork()) {
+ case -1:
+ fprintf(stderr, "error: failed to daemonize\n");
+ syslog(LOG_NOTICE, "error: failed to daemonize");
+ return 1;
+ case 0:
+ if (setsid() == -1)
+ return 1;
+ fclose(stdout);
+ fclose(stderr);
+ break;
+ default:
+ return 0;
}
} else if (!strcmp("-f", argv[i])) {
if (argv[i+1] == NULL || argv[i+1][0] == '-') {
@@ -43,7 +51,7 @@ int main(int argc, char *argv[]) {
}
}
- for (;; sleep(60)) {
+ for (t = time(NULL), sleep(60 - t % 60); 1; t = time(NULL), sleep(60 - t % 60)) {
t = time(NULL);
tm = localtime(&t);
@@ -54,7 +62,7 @@ int main(int argc, char *argv[]) {
}
for (i = 0; fgets(line, LEN+1, fp) != NULL; i++) {
- if (line[1] == '\0' || line[0] == '\043')
+ if (line[0] == '#' || line[1] == '\0')
continue;
for (j = 0, col = strtok(line,"\t"); col != NULL; j++, col = strtok(NULL, "\t")) {
@@ -67,11 +75,12 @@ int main(int argc, char *argv[]) {
} else if (j == 5) {
printf("run: %s", col);
syslog(LOG_NOTICE, "run: %s", col);
- if (system(col) != 0) {
+ if (fork() == 0) {
+ execl("/bin/sh", "/bin/sh", "-c", col, (char *)NULL);
fprintf(stderr, "error: job failed\n");
syslog(LOG_NOTICE, "error: job failed");
}
- } else if (!isdigit(col[0])) {
+ } else if (!isdigit(col[0]) || j > 5) {
fprintf(stderr, "error: %s line %d column %d\n", config, i+1, j+1);
syslog(LOG_NOTICE, "error: %s line %d column %d", config, i+1, j+1);
}