Member-only story
Building a Cron-Based Task Runner in Go With JSON Configs
2 min readApr 19, 2025
If you’ve ever needed to automate scheduled tasks like sending reports, syncing data, or cleaning up logs — but didn’t want to pull in a full-blown task queue — Go and a little cron logic can get you surprisingly far.
Why Use a Custom Task Runner?
- 🔁 Fine-grained control over scheduling
- 🔧 Lightweight and embeddable
- 📄 Easy to configure via JSON
Step 1: Define Your Task Model
Create a simple task.json
file that defines what needs to run and when:
[
{
"name": "daily_report",
"schedule": "0 9 * * *",
"command": "generate-report"
},
{
"name": "cleanup_logs",
"schedule": "30 2 * * 1",
"command": "clear-logs"
}
]
Step 2: Load Tasks From JSON
type Task struct {
Name string `json:"name"`
Schedule string `json:"schedule"`
Command string `json:"command"`
}
func LoadTasks(filename string) ([]Task, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var tasks []Task
if err := json.Unmarshal(data, &tasks); err != nil {
return nil, err
}
return tasks, nil
}