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 and year,

  • - is used to define value range, with min and max boundaries, min-max or max-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

0 * * * * * *

CRON is valid at the beginning of each minute

* * * * * 2 *

CRON is valid every Tuesday all day long

0 0 13-15 * * 2-4 *

CRON is valid every beginning of the minute between hours 13-15 afternoon, between Tuesday and Thursday

*/5 * * * * * *

CRON is valid every 5 seconds starting at 0

*/5 */5 * * * * *

CRON is valid every 5 seconds each 5 minutes, from 00:00 to 55:55

0 0 0 * * 5 *

Every Friday at midnight

0 0 */2 * * * *

Every 2 hours at beginning of the hour

* * */2 * * * *

Every second of every minute every 2 hours (0, 2, 4, .., 22)

0 0 0 * * 1-5 *

At midnight, 00:00 every week between Monday and Friday

15 23 */6 * * * *

Every 6 hours at (min:sec) 23:15 (00:23:15, 06:23:15, 12:23:15, …)

0 0 0 1 * * *

At 00:00:00 beginning of the month

0 0 0 1 */3 * *

Every beginning of the quarter at 00:00:00

10 15 20 * 8 6 *

At 20:15:20 every Saturday in August

10 15 20 8 * 6 *

At 20:15:20 every Saturday that is also 8th day in month (both must match, day Saturday and date 8th)

30-45 * * * * * *

Every second between 30 and 45

30-45/3 * * * * * *

Every 3rd second in every minute, when seconds are between 30 and 45

0 23/1 * * * * *

Every beginning of a minute when minute is between 23 and 59

50-10 * * * * * *

Every second when seconds are from 50-59 and 00-10 (overflow mode)

Basic CRON example with parser
 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}