Lesson 28
Scheduled Tasks and Automation
AI-generated
Scheduled Tasks and Automation
Schedule Claude Code to run at specific times
Use the /loop skill for periodic tasks
Create cron jobs that invoke Claude
Build notification systems for long-running tasks
Know when automation adds value versus complexity
Claude Code does not have to run only when you are watching. You can schedule it to run at specific times, loop on tasks indefinitely, or trigger it from external events. This opens up use cases like overnight batch processing, periodic code maintenance, and automated monitoring.
This lesson covers scheduling and automation patterns for Claude Code.
The simplest form of automation is the built-in /loop skill:
This runs the prompt every 5 minutes until you stop it.
Interval formats:
5m - 5 minutes
1h - 1 hour
30s - 30 seconds
Examples:
Check token usage every 10 minutes.
Hourly test runs.
Health monitoring.
Stopping loops:
Press Escape or Ctrl+C to stop a running loop.
Limitations:
The loop runs in your current session. If you close the terminal, the loop stops. For persistent automation, use cron or external schedulers.
For automation that persists beyond your session, use cron:
Create a shell script:
Schedule with cron:
Add:
This runs at 2 AM daily.
Cron format:
Examples:
0 * * * * - Every hour
0 9 * * 1-5 - 9 AM on weekdays
0 0 * * 0 - Midnight on Sundays
Build scripts that run Claude for specific tasks:
Daily lint fix:
Weekly dependency update:
Overnight batch processing:
For long-running or scheduled tasks, notifications help you stay informed:
Email notification:
Slack notification:
Desktop notification (macOS):
Log files:
Append output to a log file for later review.
Trigger Claude based on events rather than schedules:
File watcher:
Git hook:
Create .git/hooks/post-commit:
Webhook receiver:
Build a simple server that triggers Claude:
For production automation on Linux, use systemd:
Create a service file:
Enable and start:
Automated tasks can run up costs quickly:
Limit frequency:
Do not run expensive tasks more often than needed. Daily is usually enough for maintenance tasks.
Use cheaper models:
Haiku costs less for simple tasks.
Scope narrowly:
Narrow scope reduces tokens.
Set timeouts:
Kill after 5 minutes to prevent runaway costs.
Budget monitoring:
Check your Anthropic dashboard regularly. Set up billing alerts.
Automated scripts need robust error handling:
Check exit codes:
Retry logic:
Logging:
Use /loop for simple in-session periodic tasks
Use cron for persistent scheduled automation
Headless mode (-p and --yes) enables scripted execution
Add notifications for visibility into automated tasks
Event-driven automation responds to file changes, webhooks, or git hooks
Control costs with narrow scope, cheaper models, and timeouts
Handle errors with retries, logging, and alerts
Systemd provides production-grade service management
Create a simple automated task:
Create a script at ~/scripts/claude-status.sh:
Make it executable: chmod +x ~/scripts/claude-status.sh
Test it manually: ~/scripts/claude-status.sh
Check the log: cat ~/claude-status.log
Optionally add to cron for hourly runs:
Remove from cron when done experimenting.
This exercise introduces scripted automation.
https://code.claude.com/docs/en/automation - Automation guide
https://code.claude.com/docs/en/headless - Headless mode reference
https://code.claude.com/docs/en/skills#loop - Loop skill documentation