scron

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

commit 03cdcc59b01d156c53f8e4a881972717f4574278
parent bf9345738769e9c4691014420c5ca260a8c78241
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Tue, 10 Jun 2014 16:43:26 +0300

first commit

Diffstat:
AREADME | 3+++
DREADME.md | 4----
Acrontab | 6++++++
Adcron.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/README b/README @@ -0,0 +1,3 @@ +dcron - simple cron + +this is early prototype diff --git a/README.md b/README.md @@ -1,4 +0,0 @@ -dcron -===== - -simple cron diff --git a/crontab b/crontab @@ -0,0 +1,6 @@ +# 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 @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#define MAXLEN 100 + +int main(void) { + FILE *fp; + char line[MAXLEN+1]; + char *col; + char cmd[MAXLEN+1]; + int i, min, hour; + time_t rawtime; + struct tm *tmtime; + + while (1) { + rawtime = time(NULL); + tmtime = localtime(&rawtime); + + printf("dcron %.2d:%.2d %.2d.%.2d.%.4d\n", + tmtime->tm_hour, tmtime->tm_min, + tmtime->tm_mday, tmtime->tm_mon, tmtime->tm_year+1900); + + fp = fopen("crontab", "r"); + if (fp == NULL) { + fprintf(stderr, "cant read crontab\n"); + exit(1); + } + + while (fgets(line, MAXLEN+1, fp) != NULL) { + if (line[1] == '\0') { + // empty line + } else if (line[0] == '\043') { + // comment + } else { + // split line + i = 0; + col = strtok (line,"\t"); + while (col != NULL) { + // parse tasks + switch (i) { + case 0: + min = strtol(col, NULL, 0); + break; + case 1: + hour = strtol(col, NULL, 0); + 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); + } + } + } + + sleep(60); + } + + fclose(fp); + return 0; +}