AI-101

Lesson 28

Scheduled Tasks and Automation

AI Confidence: 90%

AI-generated

Scheduled Tasks and Automation

Learning Objectives

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

Introduction

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 /loop Skill

The simplest form of automation is the built-in /loop skill:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

This runs the prompt every 5 minutes until you stop it.

Interval formats:

5m - 5 minutes

1h - 1 hour

30s - 30 seconds

Examples:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Check token usage every 10 minutes.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Hourly test runs.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

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.

Cron Jobs

For automation that persists beyond your session, use cron:

Create a shell script:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Schedule with cron:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Add:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

This runs at 2 AM daily.

Cron format:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Examples:

0 * * * * - Every hour

0 9 * * 1-5 - 9 AM on weekdays

0 0 * * 0 - Midnight on Sundays

Headless Automation Scripts

Build scripts that run Claude for specific tasks:

Daily lint fix:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Weekly dependency update:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Overnight batch processing:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop
Notifications

For long-running or scheduled tasks, notifications help you stay informed:

Email notification:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Slack notification:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Desktop notification (macOS):

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Log files:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Append output to a log file for later review.

Event-Driven Automation

Trigger Claude based on events rather than schedules:

File watcher:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Git hook:

Create .git/hooks/post-commit:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Webhook receiver:

Build a simple server that triggers Claude:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop
Systemd Services

For production automation on Linux, use systemd:

Create a service file:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Enable and start:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop
Cost Control for Automation

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:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Haiku costs less for simple tasks.

Scope narrowly:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Narrow scope reduces tokens.

Set timeouts:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Kill after 5 minutes to prevent runaway costs.

Budget monitoring:

Check your Anthropic dashboard regularly. Set up billing alerts.

Error Handling

Automated scripts need robust error handling:

Check exit codes:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Retry logic:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Logging:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop
Key Takeaways

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

Try It Yourself

Create a simple automated task:

Create a script at ~/scripts/claude-status.sh:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

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:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Remove from cron when done experimenting.

This exercise introduces scripted automation.

Sources

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