scron

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

commit 2be597ef6b18138429257bad6ea31684ce4cd942
parent b55d770c556ed9216e544d1fc03bd7e73bd6b932
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Sun, 15 Jun 2014 15:22:11 +0300

changing name to scron

Diffstat:
MMakefile | 18+++++++++---------
MREADME | 24+++++++++++++++++-------
Acron.c | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rdcron.conf -> crontab | 0
Ddcron.c | 93-------------------------------------------------------------------------------
Ainit.d/cron | 5+++++
Dinit.d/dcron | 5-----
7 files changed, 124 insertions(+), 114 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,18 +1,18 @@ CFLAGS=-std=c99 -Wall -Wpedantic -Wextra -all: dcron +all: cron -install: dcron +install: cron mkdir -p ${DESTDIR}/sbin mkdir -p ${DESTDIR}/etc/init.d - install -m 755 dcron ${DESTDIR}/sbin/ - install -m 755 init.d/dcron ${DESTDIR}/etc/init.d/ - install -m 644 dcron.conf ${DESTDIR}/etc/ + install -m 755 cron ${DESTDIR}/sbin/ + install -m 755 init.d/cron ${DESTDIR}/etc/init.d/ + install -m 644 crontab ${DESTDIR}/etc/ uninstall: - rm -f ${DESTDIR}/sbin/dcron - rm ${DESTDIR}/etc/init.d/dcron - rm ${DESTDIR}/etc/dcron.conf + rm -f ${DESTDIR}/sbin/cron + rm ${DESTDIR}/etc/init.d/cron + rm ${DESTDIR}/etc/crontab clean: - rm dcron + rm cron diff --git a/README b/README @@ -1,16 +1,26 @@ -dcron - simple cron daemon +scron - simple cron daemon +========================== -features: +about +----- +author: Ari Malinen +license: BSD 2-clause +git: https://github.com/defer-/scron + +features +-------- +Schedule tasks. Single daemon, config and initscript. -Logs to stdout and syslog. +Log to stdout and syslog. -usage: -usage: dcron [options] +usage +----- +usage: cron [options] -h help -d daemon -f <file> config file -config: +config +------ # tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command 0 5 * * * updatedb -9 5 * * * mandb --quiet diff --git a/cron.c b/cron.c @@ -0,0 +1,93 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <time.h> +#include <unistd.h> +#include <syslog.h> + +#define LEN 80 + +int main(int argc, char *argv[]) { + char *ep, *col, line[LEN+1], config[LEN+1] = "/etc/crontab"; + int i, j; + time_t t; + struct tm *tm; + FILE *fp; + + openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1); + + for (i = 1; i < argc; i++) { + if (!strcmp("-d", argv[i])) { + if (fork() == 0) { + setsid(); + fclose(stdin); + fclose(stdout); + fclose(stderr); + } else { + return 0; + } + } else if (!strcmp("-f", argv[i])) { + if (argv[i+1] == NULL || argv[i+1][0] == '-') { + fprintf(stderr, "error: -f needs parameter\n"); + syslog(LOG_NOTICE, "error: -f needs parameter"); + return 1; + } + strncpy(config, argv[++i], LEN); + } 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) { + t = time(NULL); + sleep(60 - t % 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); + continue; + } + + for (i = 0; fgets(line, LEN+1, fp); i++) { + if (line[0] == '#' || line[0] == '\n') + continue; + + for (j = 0, col = strtok(line,"\t"); col; j++, col = strtok(NULL, "\t")) { + ep = ""; + + if ((j == 0 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_min) && *ep == '\0') || + (j == 1 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_hour) && *ep == '\0') || + (j == 2 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mday) && *ep == '\0') || + (j == 3 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mon) && *ep == '\0') || + (j == 4 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_wday) && *ep == '\0')) { + continue; + } else if (j == 5) { + printf("run: %s", col); + syslog(LOG_NOTICE, "run: %s", col); + if (fork() == 0) { + execl("/bin/sh", "/bin/sh", "-c", col, (char *) NULL); + fprintf(stderr, "error: job failed: %s", col); + syslog(LOG_NOTICE, "error: job failed: %s", col); + } + } else if (!isdigit(col[0]) || *col < 0 || *col > 59 || *ep != '\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; + } + } + + fclose(fp); + } + + closelog(); + return 0; +} diff --git a/dcron.conf b/crontab diff --git a/dcron.c b/dcron.c @@ -1,93 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> -#include <time.h> -#include <unistd.h> -#include <syslog.h> - -#define LEN 80 - -int main(int argc, char *argv[]) { - char *ep, *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 = 1; i < argc; i++) { - if (!strcmp("-d", argv[i])) { - if (fork() == 0) { - setsid(); - fclose(stdin); - fclose(stdout); - fclose(stderr); - } else { - return 0; - } - } else if (!strcmp("-f", argv[i])) { - if (argv[i+1] == NULL || argv[i+1][0] == '-') { - fprintf(stderr, "error: -f needs parameter\n"); - syslog(LOG_NOTICE, "error: -f needs parameter"); - return 1; - } - strncpy(config, argv[++i], LEN); - } 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) { - t = time(NULL); - sleep(60 - t % 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); - continue; - } - - for (i = 0; fgets(line, LEN+1, fp); i++) { - if (line[0] == '#' || line[0] == '\n') - continue; - - for (j = 0, col = strtok(line,"\t"); col; j++, col = strtok(NULL, "\t")) { - ep = ""; - - if ((j == 0 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_min) && *ep == '\0') || - (j == 1 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_hour) && *ep == '\0') || - (j == 2 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mday) && *ep == '\0') || - (j == 3 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mon) && *ep == '\0') || - (j == 4 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_wday) && *ep == '\0')) { - continue; - } else if (j == 5) { - printf("run: %s", col); - syslog(LOG_NOTICE, "run: %s", col); - if (fork() == 0) { - execl("/bin/sh", "/bin/sh", "-c", col, (char *) NULL); - fprintf(stderr, "error: job failed: %s", col); - syslog(LOG_NOTICE, "error: job failed: %s", col); - } - } else if (!isdigit(col[0]) || *col < 0 || *col > 59 || *ep != '\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; - } - } - - fclose(fp); - } - - closelog(); - return 0; -} diff --git a/init.d/cron b/init.d/cron @@ -0,0 +1,5 @@ +#!/sbin/runscript + +command=cron +command_args=-d +name="cron" diff --git a/init.d/dcron b/init.d/dcron @@ -1,5 +0,0 @@ -#!/sbin/runscript - -command=dcron -command_args=-d -name="dcron"