scron

simple cron daemon
git clone git://git.2f30.org/scron
Log | Files | Refs | README | LICENSE

commit 38137b33f3cc2ef295488af11ec0b3b66e2eae30
parent 85ed137c538eebb9f5dee3e6c9488f9ce143f7f2
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Thu, 12 Jun 2014 17:09:12 +0300

cleaned code

Diffstat:
MREADME | 2++
Mdcron.c | 63+++++++++++++++++++++++++++------------------------------------
2 files changed, 29 insertions(+), 36 deletions(-)

diff --git a/README b/README @@ -3,6 +3,8 @@ 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] diff --git a/dcron.c b/dcron.c @@ -10,23 +10,16 @@ #define LEN 100 int main(int argc, char *argv[]) { - char config[LEN+1] = "/etc/dcron.conf"; - char line[LEN+1], cmd[LEN+1], *col; - int i, j, k[5]; + char *col, line[LEN+1], config[LEN+1] = "/etc/dcron.conf"; + int i, j; time_t t; struct tm *tm; FILE *fp; openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1); - for (i = 0; i < argc; i++) { - if (!strcmp("-h", argv[i])) { - fprintf(stderr, "usage: %s [options]\n", argv[0]); - fprintf(stderr, "-h help\n"); - fprintf(stderr, "-d daemon\n"); - fprintf(stderr, "-f <file> config file\n"); - return 1; - } else if (!strcmp("-d", argv[i])) { + 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"); @@ -41,17 +34,23 @@ int main(int argc, char *argv[]) { strncpy(config, argv[++i], LEN); printf("config: %s\n", config); syslog(LOG_NOTICE, "config: %s", config); + } else { + fprintf(stderr, "usage: %s [options]\n", argv[0]); + fprintf(stderr, "-h help\n"); + fprintf(stderr, "-d daemon\n"); + fprintf(stderr, "-f <file> config file\n"); + return 1; } } - while (1) { + for (;; sleep(60)) { t = time(NULL); tm = localtime(&t); if ((fp = fopen(config, "r")) == NULL) { fprintf(stderr, "error: cant read %s\n", config); syslog(LOG_NOTICE, "error: cant read %s", config); - return 1; + continue; } for (i = 0; fgets(line, LEN+1, fp) != NULL; i++) { @@ -59,35 +58,27 @@ int main(int argc, char *argv[]) { continue; for (j = 0, col = strtok(line,"\t"); col != NULL; j++, col = strtok(NULL, "\t")) { - if (j == 5) - strncpy(cmd, col, LEN); - else if (isdigit(col[0])) - k[j] = strtol(col, NULL, 0); - else if (ispunct(col[0])) - k[j] = -1; - else { - k[j] = -2; + if ((j == 0 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_min)) || + (j == 1 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_hour)) || + (j == 2 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_mday)) || + (j == 3 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_mon)) || + (j == 4 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_wday))) { + continue; + } else if (j == 5) { + printf("run: %s", col); + syslog(LOG_NOTICE, "run: %s", col); + if (system(col) != 0) { + fprintf(stderr, "error: job failed\n"); + syslog(LOG_NOTICE, "error: job failed"); + } + } else if (!isdigit(col[0])) { 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); - break; - } - } - - if ((k[0] == -1 || k[0] == tm->tm_min) && - (k[1] == -1 || k[1] == tm->tm_hour) && - (k[2] == -1 || k[2] == tm->tm_mday) && - (k[3] == -1 || k[3] == tm->tm_mon) && - (k[4] == -1 || k[4] == tm->tm_wday)) { - printf("run: %s", cmd); - syslog(LOG_NOTICE, "run: %s", cmd); - if (system(cmd) != 0) { - fprintf(stderr, "error: job failed\n"); - syslog(LOG_NOTICE, "error: job failed"); } + break; } } fclose(fp); - sleep(60); } closelog(); return 0;