scron

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

commit fb1b68d21af265e715b7388bec66bf3cbc816275
parent 6c0ea8dd9b2f06b913f0fc6292610b41e4bb846f
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Tue, 10 Jun 2014 21:43:29 +0300

sigint handling

Diffstat:
Mdcron.c | 25+++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/dcron.c b/dcron.c @@ -3,6 +3,7 @@ #include <string.h> #include <time.h> #include <ctype.h> +#include <signal.h> #include <unistd.h> #include <syslog.h> @@ -11,8 +12,16 @@ static const char config[] = "/etc/dcron.conf"; +FILE *fp; + +void inthandler(void) { + syslog(LOG_NOTICE, "quit"); + closelog(); + fclose(fp); + exit(0); +} + int main(int argc, char *argv[]) { - FILE *fp; char line[MAXLEN+1]; char *col; int min, hour, mday, mon, wday; @@ -25,8 +34,11 @@ int main(int argc, char *argv[]) { fprintf(stderr, "usage: %s [-h = help] [-d = daemon]\n", argv[0]); return 1; } else if (argc > 1 && !strcmp("-d", argv[1])) { - daemon(1, 0); + if (!daemon(1, 0)) + return 1; } + + signal(SIGINT, inthandler); openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1); syslog(LOG_NOTICE, "start uid:%d", getuid()); @@ -42,6 +54,8 @@ int main(int argc, char *argv[]) { continue; } + min = hour = mday = mon = wday = 0; + while (fgets(line, MAXLEN+1, fp) != NULL) { if (line[1] != '\0' && line[0] != '\043') { col = strtok(line,"\t"); @@ -97,7 +111,8 @@ int main(int argc, char *argv[]) { printf("run: %s", cmd); syslog(LOG_NOTICE, "run: %s", cmd); - system(cmd); + if (system(cmd)) + puts("ok"); } } } @@ -107,8 +122,6 @@ int main(int argc, char *argv[]) { } sleep(SLEEP); } - syslog(LOG_NOTICE, "quit"); - closelog(); - fclose(fp); + inthandler(); return 0; }