Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Go
7,169
204 commits
updated Sep 29, 2026
gocron is a job scheduling package which lets you run Go functions at pre-determined intervals.
Looking for a visual interface? Check out gocron-ui — a lightweight web dashboard to monitor, trigger, and manage your
gocronjobs in real time.
If you want to chat, you can find us on Slack at
go get github.com/go-co-op/gocron/v2
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron/v2"
)
func main() {
// create a scheduler
s, err := gocron.NewScheduler()
if err != nil {
// handle error
}
// add a job to the scheduler
j, err := s.NewJob(
gocron.DurationJob(
10*time.Second,
),
gocron.NewTask(
func(a string, b int) {
// do things
},
"hello",
1,
),
)
if err != nil {
// handle error
}
// each job has a unique id
fmt.Println(j.ID())
// start the scheduler
s.Start()
// block until you are ready to shut down
select {
case <-time.After(time.Minute):
}
// when you're done, shut it down
err = s.Shutdown()
// or for context-aware teardown:
// err = s.ShutdownWithContext(ctx)
if err != nil {
// handle error
}
}
Community articles and tutorials about using gocron:
Jobs can be run at various intervals.
time.Duration.time.Duration between a min and max.Jobs can be scheduled with different interval timing modes.
Jobs can be limited individually or across the entire scheduler.
Multiple instances of gocron can be run.
Job events can trigger actions.
Many job and scheduler options are available.
NewJob.NewScheduler
and the WithGlobalJobOptions option.NewScheduler.Logs can be enabled.
Metrics may be collected from the execution of each job and scheduler lifecycle events.
Monitor: A monitor can be used to collect metrics for each job from a scheduler.
MonitorStatus: Extends Monitor with status and error tracking for each job.
SchedulerMonitor: A scheduler monitor provides comprehensive observability into scheduler and job lifecycle events.
Available Metrics:
SchedulerStarted, SchedulerStopped, SchedulerShutdownJobRegistered, JobUnregistered - track jobs added/removed from schedulerJobStarted, JobRunning, JobCompleted, JobFailed - monitor job execution flowJobExecutionTime, JobSchedulingDelay - measure job duration and scheduling lagConcurrencyLimitReached - detect when singleton or limit mode constraints are hitDerived Metrics (calculable from events):
JobFailed / (JobCompleted + JobFailed)JobExecutionTime eventsJobRegistered - JobUnregisteredJobStarted - (JobCompleted + JobFailed)Example - Prometheus Integration:
type PrometheusMonitor struct {
jobsCompleted prometheus.Counter
jobsFailed prometheus.Counter
executionTime prometheus.Histogram
schedulingDelay prometheus.Histogram
}
func (p *PrometheusMonitor) JobExecutionTime(job gocron.Job, duration time.Duration) {
p.executionTime.Observe(duration.Seconds())
}
func (p *PrometheusMonitor) JobSchedulingDelay(job gocron.Job, scheduled, actual time.Time) {
if delay := actual.Sub(scheduled); delay > 0 {
p.schedulingDelay.Observe(delay.Seconds())
}
}
// Initialize scheduler with monitor
s, _ := gocron.NewScheduler(gocron.WithSchedulerMonitor(monitor))
Use Cases: Prometheus metrics, custom dashboards, alerting systems, performance monitoring
The gocron library is set up to enable testing.
We appreciate the support for free and open source software!
This project is supported by:
(top 24 of 60)
586 followers · starred Dec 2023
238 followers · starred Oct 2024
3,221 followers · starred Sep 2023
115 followers · starred May 2024
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Go
7,169
204 commits
updated Sep 29, 2026
gocron is a job scheduling package which lets you run Go functions at pre-determined intervals.
Looking for a visual interface? Check out gocron-ui — a lightweight web dashboard to monitor, trigger, and manage your
gocronjobs in real time.
If you want to chat, you can find us on Slack at
go get github.com/go-co-op/gocron/v2
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron/v2"
)
func main() {
// create a scheduler
s, err := gocron.NewScheduler()
if err != nil {
// handle error
}
// add a job to the scheduler
j, err := s.NewJob(
gocron.DurationJob(
10*time.Second,
),
gocron.NewTask(
func(a string, b int) {
// do things
},
"hello",
1,
),
)
if err != nil {
// handle error
}
// each job has a unique id
fmt.Println(j.ID())
// start the scheduler
s.Start()
// block until you are ready to shut down
select {
case <-time.After(time.Minute):
}
// when you're done, shut it down
err = s.Shutdown()
// or for context-aware teardown:
// err = s.ShutdownWithContext(ctx)
if err != nil {
// handle error
}
}
Community articles and tutorials about using gocron:
Jobs can be run at various intervals.
time.Duration.time.Duration between a min and max.Jobs can be scheduled with different interval timing modes.
Jobs can be limited individually or across the entire scheduler.
Multiple instances of gocron can be run.
Job events can trigger actions.
Many job and scheduler options are available.
NewJob.NewScheduler
and the WithGlobalJobOptions option.NewScheduler.Logs can be enabled.
Metrics may be collected from the execution of each job and scheduler lifecycle events.
Monitor: A monitor can be used to collect metrics for each job from a scheduler.
MonitorStatus: Extends Monitor with status and error tracking for each job.
SchedulerMonitor: A scheduler monitor provides comprehensive observability into scheduler and job lifecycle events.
Available Metrics:
SchedulerStarted, SchedulerStopped, SchedulerShutdownJobRegistered, JobUnregistered - track jobs added/removed from schedulerJobStarted, JobRunning, JobCompleted, JobFailed - monitor job execution flowJobExecutionTime, JobSchedulingDelay - measure job duration and scheduling lagConcurrencyLimitReached - detect when singleton or limit mode constraints are hitDerived Metrics (calculable from events):
JobFailed / (JobCompleted + JobFailed)JobExecutionTime eventsJobRegistered - JobUnregisteredJobStarted - (JobCompleted + JobFailed)Example - Prometheus Integration:
type PrometheusMonitor struct {
jobsCompleted prometheus.Counter
jobsFailed prometheus.Counter
executionTime prometheus.Histogram
schedulingDelay prometheus.Histogram
}
func (p *PrometheusMonitor) JobExecutionTime(job gocron.Job, duration time.Duration) {
p.executionTime.Observe(duration.Seconds())
}
func (p *PrometheusMonitor) JobSchedulingDelay(job gocron.Job, scheduled, actual time.Time) {
if delay := actual.Sub(scheduled); delay > 0 {
p.schedulingDelay.Observe(delay.Seconds())
}
}
// Initialize scheduler with monitor
s, _ := gocron.NewScheduler(gocron.WithSchedulerMonitor(monitor))
Use Cases: Prometheus metrics, custom dashboards, alerting systems, performance monitoring
The gocron library is set up to enable testing.
We appreciate the support for free and open source software!
This project is supported by:
(top 24 of 60)
586 followers · starred Dec 2023
238 followers · starred Oct 2024
3,221 followers · starred Sep 2023
115 followers · starred May 2024