Overview
Unlike the heartbeat system which checks for tasks periodically, the cron service allows you to schedule tasks with exact timing control. Jobs are persisted to disk and survive restarts. Key features:- Three schedule types:
at(one-time),every(recurring interval),cron(cron expressions) - Persistent job storage in JSON format
- Automatic job state management (next run time, last status, error tracking)
- Optional result delivery to chat channels
- Timezone support for cron expressions
- Hot-reload when jobs file is modified externally
Schedule Types
1. One-Time (at)
Execute a task at a specific timestamp:
2. Recurring Interval (every)
Repeat a task at fixed intervals:
3. Cron Expression (cron)
Use standard cron expressions for complex schedules:
Data Structures
CronSchedule
Fromnanobot/cron/types.py:8-18:
CronPayload
Fromnanobot/cron/types.py:21-29:
CronJob
Fromnanobot/cron/types.py:41-52:
Core Implementation
Next Run Computation
Fromnanobot/cron/service.py:20-46:
Job Execution
Fromnanobot/cron/service.py:245-276:
Usage
Initialization
Adding Jobs
Managing Jobs
Hot-Reload
The service automatically detects whenjobs.json is modified externally and reloads it (nanobot/cron/service.py:78-86):
Use Cases
1. Daily Standup Preparation
2. Server Health Check
3. Weekly Report Generation
4. Meeting Reminder with Lead Time
5. Backup Task
Persistence Format
Jobs are stored in JSON format at~/.nanobot/workspace/cron/jobs.json:
Best Practices
- Use meaningful names: Job names appear in logs and help debugging
- Set appropriate timezones: Always specify
tzfor cron expressions to avoid ambiguity - Monitor job status: Check
last_statusandlast_errorto catch failures - Use
delete_after_runfor one-time tasks: Automatically clean up completed one-shot jobs - Test with manual runs: Use
run_job(force=True)to test before relying on schedule - Handle errors gracefully: Job execution errors are logged but don’t stop the service
- Consider delivery settings: Set
deliver=Trueonly when you want channel notifications
Validation
Schedule validation prevents non-runnable jobs (nanobot/cron/service.py:49-61):
Stopping the Service
Related
- Heartbeat System - Periodic task checking without precise timing
- Subagents - Background task execution