commit 9abf25f9ddbf6fec424115e9873b79dd033e48e7
parent 3d3ff2515bc16f6db71df32efa1cdf903d068be2
Author: Ari Malinen <ari.malinen@gmail.com>
Date: Tue, 10 Jun 2014 19:52:46 +0300
support for syslog
Diffstat:
3 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/README b/README
@@ -3,17 +3,10 @@ dcron - simple cron daemon
This tool is not ready for use!
info:
-Single daemon, single config file. One task per line. Tab is separator.
+Single daemon, single config file.
+Logs to stdout and syslog.
-example:
+config:
# min,hour,mday,mon,wday,command
* * * * * exec echo minute passed
-30 * * * * exec echo 30 min passed
-
-output:
-dcron 18:53 10.05.2014
-run: exec echo minute passed
-minute passed
-dcron 18:54 10.05.2014
-run: exec echo minute passed
-minute passed
+30 * * * * exec echo its half past
diff --git a/cron.conf b/cron.conf
@@ -1,3 +1,3 @@
# min,hour,mday,mon,wday,command
* * * * * exec echo minute passed
-30 * * * * exec echo 30 min passed
+30 * * * * exec echo its half past
diff --git a/dcron.c b/dcron.c
@@ -4,13 +4,14 @@
#include <time.h>
#include <ctype.h>
#include <unistd.h>
+#include <syslog.h>
#define MAXLEN 100
#define SLEEP 60
static const char config[] = "cron.conf";
-int main(void) {
+int main(int argc, char *argv[]) {
FILE *fp;
char line[MAXLEN+1];
char *col;
@@ -20,14 +21,20 @@ int main(void) {
time_t rawtime;
struct tm *tmtime;
+ if (argc > 1 && !strcmp("-h", argv[1])) {
+ fprintf(stderr, "usage: %s [-h = help] [-d = daemon]\n", argv[0]);
+ return 1;
+ } else if (argc > 1 && !strcmp("-d", argv[1])) {
+ daemon(1, 0);
+ }
+
+ openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1);
+ syslog(LOG_NOTICE, "started by user %d", getuid());
+
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(config, "r");
if (fp == NULL) {
fprintf(stderr, "error: cant read %s\n", config);
@@ -84,7 +91,12 @@ int main(void) {
if (mday == -1 || mday == tmtime->tm_mday) {
if (mon == -1 || mon == tmtime->tm_mon) {
if (wday == -1 || wday == tmtime->tm_wday) {
+ printf("dcron %.2d:%.2d %.2d.%.2d.%.4d\n",
+ tmtime->tm_hour, tmtime->tm_min,
+ tmtime->tm_mday, tmtime->tm_mon, tmtime->tm_year+1900);
+
printf("run: %s", cmd);
+ syslog(LOG_NOTICE, "run: %s", cmd);
system(cmd);
}
}
@@ -95,6 +107,7 @@ int main(void) {
}
sleep(SLEEP);
}
+ closelog();
fclose(fp);
return 0;
}