scron

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

commit 1972b52dc1d5ffc7c0f2c4ab369c73ffb4bc58d1
parent 03cdcc59b01d156c53f8e4a881972717f4574278
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Tue Jun 10 17:52:38 +0300

detect min,hour,mday,mon and wday

Diffstat:
Makefile | 16++++++++++++++++
cron.conf | 4++++
crontab | 6------
dcron.c | 63++++++++++++++++++++++++++++++++++++++++++++++++---------------
4 files changed, 68 insertions(+), 21 deletions(-)
diff --git a/Makefile b/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-Wall +PREFIX=/usr +BINDIR=${PREFIX}/bin + +all: dcron + +install: dcron + mkdir -p ${DESTDIR}${BINDIR} + install -m 755 dcron ${DESTDIR}${BINDIR} + +uninstall: + rm -f ${DESTDIR}${BINDIR}/dcron + +clean: + rm dcron diff --git a/cron.conf b/cron.conf @@ -0,0 +1,4 @@ +# min,hour,mday,mon,wday,command +39 17 * * * exec nice emerge --sync --quiet +40 * * * * exec nice mandb --quiet +41 * * * * exec ionice -n2 updatedb diff --git a/crontab b/crontab @@ -1,6 +0,0 @@ -# dcron - -# minute, hour, day of month, month, day of week, command -41 16 * * * exec nice emerge --sync --quiet -42 16 * * * exec nice mandb --quiet -43 16 * * * exec ionice -n2 updatedb diff --git a/dcron.c b/dcron.c @@ -2,16 +2,19 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <ctype.h> #include <unistd.h> #define MAXLEN 100 +static const char config[] = "cron.conf"; + int main(void) { FILE *fp; char line[MAXLEN+1]; - char *col; char cmd[MAXLEN+1]; - int i, min, hour; + char *col; + int i, min, hour, mday, mon, wday; time_t rawtime; struct tm *tmtime; @@ -23,10 +26,11 @@ int main(void) { tmtime->tm_hour, tmtime->tm_min, tmtime->tm_mday, tmtime->tm_mon, tmtime->tm_year+1900); - fp = fopen("crontab", "r"); + fp = fopen(config, "r"); if (fp == NULL) { - fprintf(stderr, "cant read crontab\n"); - exit(1); + fprintf(stderr, "error: cant read %s\n", config); + sleep(60); + continue; } while (fgets(line, MAXLEN+1, fp) != NULL) { @@ -37,35 +41,64 @@ int main(void) { } else { // split line i = 0; - col = strtok (line,"\t"); + col = strtok(line,"\t"); while (col != NULL) { - // parse tasks switch (i) { case 0: - min = strtol(col, NULL, 0); + if (isalnum(col[0])) + min = strtol(col, NULL, 0); + else + min = -1; break; case 1: - hour = strtol(col, NULL, 0); + if (isalnum(col[0])) + hour = strtol(col, NULL, 0); + else + hour = -1; + break; + case 2: + if (isalnum(col[0])) + mday = strtol(col, NULL, 0); + else + mday = -1; + break; + case 3: + if (isalnum(col[0])) + mon = strtol(col, NULL, 0); + else + mon = -1; + break; + case 4: + if (isalnum(col[0])) + wday = strtol(col, NULL, 0); + else + wday = -1; break; case 5: strncpy(cmd, col, MAXLEN+1); break; } - col = strtok (NULL, "\t"); i++; } - if (hour == tmtime->tm_hour && min == tmtime->tm_min) { - printf("run: %s", cmd); - // system(cmd); + // run task + if (min == -1 || min == tmtime->tm_min) { + if (hour == -1 || hour == tmtime->tm_hour) { + if (mday == -1 || mday == tmtime->tm_mday) { + if (mon == -1 || mon == tmtime->tm_mon) { + if (wday == -1 || wday == tmtime->tm_wday) { + printf("run: %s", cmd); + // system(cmd); + } + } + } + } } } } - sleep(60); } - fclose(fp); return 0; }