cron
Cron V3 has been released!
To download the specific tagged release, run:
go get github.com/robfig/cron/v3@v3.0.0
Import it in your program as:
import "github.com/robfig/cron/v3"
It requires Go 1.11 or later due to usage of Go Modules.
Refer to the documentation here:
http://godoc.org/github.com/robfig/cron
The rest of this document describes the the advances in v3 and a list of
breaking changes for users that wish to upgrade from an earlier version.
Overview
- Usage
- CRON Expression Format
- Special Characters
- Predefined schedules
- Intervals
- Time zones
- Thread safety
- Implementation
Usage
- Callers may register Funcs to be invoked on a given schedule. Cron will run them in their own goroutines.
c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
// Inspect the cron job entries' next and previous run times.
inspect(c.Entries())
..
c.Stop() // Stop the scheduler (does not stop any jobs already running).
A cron expression represents a set of times, using 6 space-separated fields.
| Field name |
Mandatory? |
Allowed values |
Allowed special characters |
| Seconds |
Yes |
0-59 |
* / , - |
| Minutes |
Yes |
0-59 |
* / , - |
| Hours |
Yes |
0-23 |
* / , - |
| Day of month |
Yes |
1-31 |
* / , - ? |
| Month |
Yes |
1-12 or JAN-DEC |
* / , - |
| Day of week |
Yes |
0-6 or SUN-SAT |
* / , - ? |
Note: Month and Day-of-week field values are case insensitive. "SUN", "Sun", and "sun" are equally accepted.
Predefined schedules ¶
You may use one of several pre-defined schedules in place of a cron expression.
| Entry |
Description |
Equivalent To |
| @yearly (or @annually) |
Run once a year, midnight, Jan. 1st |
0 0 0 1 1 * |
| @monthly |
Run once a month, midnight, first of month |
0 0 0 1 * * |
| @weekly |
Run once a week, midnight between Sat/Sun |
0 0 0 * * 0 |
| @daily (or @midnight) |
Run once a day, midnight |
0 0 0 * * * |
| @hourly |
Run once an hour, beginning of hour |
0 0 * * * * |
Intervals
You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run. This is supported by formatting the cron spec like this:
@every
where "duration" is a string accepted by time.ParseDuration (http://golang.org/pkg/time/#ParseDuration).
For example, "@every 1h30m10s" would indicate a schedule that activates after 1 hour, 30 minutes, 10 seconds, and then every interval after that.
Note: The interval does not take the job runtime into account. For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run.