Back to Nodemcu Firmware

Cron Module

docs/modules/cron.md

latest2.3 KB
Original Source

Cron Module

SinceOrigin / ContributorMaintainerSource
2016-12-18PhoeniXPhoeniXcron.c

Cron-like scheduler module.

!!! important This module needs RTC time to operate correctly. Do not forget to include the rtctime module and initialize it properly.

!!! important The cron expression has to be in GMT/UTC!

cron.schedule()

Creates a new schedule entry.

Syntax

cron.schedule(mask, callback)

Parameters

  • mask - crontab-like string mask for schedule
  • callback - callback function(entry) that is executed at the scheduled time

Returns

cron.entry sub module

Example

lua
cron.schedule("* * * * *", function(e)
  print("Every minute")
end)

cron.schedule("*/5 * * * *", function(e)
  print("Every 5 minutes")
end)

cron.schedule("0 */2 * * *", function(e)
  print("Every 2 hours")
end)

cron.reset()

Removes all scheduled entries.

Syntax

cron.reset()

Parameters

none

Returns

nil

cron.entry Module

cron.entry:handler()

Sets a new handler for entry.

Syntax

handler(callback)

Parameters

  • callback - callback function(entry) that is executed at the scheduled time

Returns

nil

Example

lua
ent = cron.schedule("* * * * *", function(e)
  print("Every minute")
end)

ent:handler(function(e)
  print("New handler: Every minute")
end)

cron.entry:schedule()

Sets a new schedule mask.

Syntax

schedule(mask)

Parameters

  • mask - crontab-like string mask for schedule

Returns

none

Example

lua
ent = cron.schedule("* * * * *", function(e)
  print("Tick")
end)

-- Every 5 minutes is really better!
ent:schedule("*/5 * * * *")

cron.entry:unschedule()

Disables schedule.

Disabled schedules may be enabled again by calling :schedule(mask).

Syntax

unschedule()

Parameters

none

Returns

nil

Example

lua
ent = cron.schedule("* * * * *", function(e)
  print("Tick")
end)

-- We don't need this anymore
ent:unschedule()