CRON
LwDTC provides ultra-lightweight support for simple CRON implementation. This page tends to provide quick information to be able to quickly move forward.
In the current revision library doesn’t support true scheduler, instead it supports CRON string parser, and dedicated function to check if CRON is valid against compared time.
Tip
User can implement its own CRON loop, that reads current time and checks if parsed CRON is valid for specific time.
Use lwdtc_cron_is_valid_for_time()
to compare
Supported characters
Numbers between minimum and maximum value for each of date and time field
Support for
seconds
,minutes
,hours
,day-in-month
,month
,day-in-week
andyear
,-
is used to define value range, with min and max boundaries,min-max
ormax-min
,
is used to specify multiple fixed values/
is used to define step between min and max values*
is used to represent any value
Note
Comparing to standard linux CRON, where fixed date in month and week day are bitwise-ORed, meaning cron will fire
on day in month match or on week day match, LwDTC does it more simple. Date&Time is valid only if both parameters are a match at the same time.
In practice, setting cron to fire on month-day = 15
and week-day = 6
, will trigger it only on 15th in a month which is also Saturday at the same time.
CRON string format
To define valid CRON string, a string with 7
parameters, separated by space, must be provided:
seconds minutes hours day-in-month month day-in-week year
Each of them has to be present to consider CRON as valid input.
CRON examples
This section provides list of some CRON examples in its default configuration.
CRON string |
Description |
---|---|
|
CRON is valid all the time, will fire every second |
|
CRON is valid at the beginning of each minute |
|
CRON is valid every Tuesday all day long |
|
CRON is valid every beginning of the minute between hours 13-15 afternoon, between Tuesday and Thursday |
|
CRON is valid every |
|
CRON is valid every |
|
Every Friday at midnight |
|
Every |
|
Every second of every minute every |
|
At midnight, |
|
Every |
|
At |
|
Every beginning of the quarter at 00:00:00 |
|
At |
|
At |
|
Every second between |
|
Every |
|
Every beginning of a minute when minute is between |
|
Every second when seconds are from |
1#include "windows.h"
2#include <time.h>
3#include <stdio.h>
4#include "lwdtc/lwdtc.h"
5
6int
7cron_basic(void) {
8 /* Define context for CRON, used to parse data to */
9 lwdtc_cron_ctx_t cron_ctx = {0};
10 struct tm* timeinfo;
11 time_t rawtime, rawtime_old = 0;
12
13 /* Execute cron to be valid every 2 seconds */
14 if (lwdtc_cron_parse(&cron_ctx, "*/2 * * * * * *") != lwdtcOK) {
15 printf("Error parsing CRON...\r\n");
16 while (1) {}
17 }
18 while (1) {
19 /* Get current time and react on changes only */
20 time(&rawtime);
21
22 /* Check if new time has changed versus last read */
23 if (rawtime != rawtime_old) {
24 rawtime_old = rawtime;
25 timeinfo = localtime(&rawtime);
26
27 /* Print time to user */
28 printf("Time: %02d.%02d.%04d %02d:%02d:%02d\r\n",
29 (int)timeinfo->tm_mday, (int)timeinfo->tm_mon, (int)timeinfo->tm_year + 1900,
30 (int)timeinfo->tm_hour, (int)timeinfo->tm_min, (int)timeinfo->tm_sec
31 );
32
33 /* Check if CRON should execute */
34 if (lwdtc_cron_is_valid_for_time(timeinfo, &cron_ctx) == lwdtcOK) {
35 printf("Executing CRON task\r\n");
36 }
37 }
38
39 /* This is sleep from windows.h lib */
40 Sleep(100);
41 }
42 return 0;
43}