Building Automated Claude Code Workers with Cron and MCP Servers
As AI development tools become more sophisticated, the opportunity to create truly autonomous coding agents is becoming a reality. In this guide, we'll explore how to build a system where Claude Code can work autonomously on tasks, fetching work from a queue, executing it, and reporting back results—all without human intervention.
The Architecture
Our automated worker system consists of several key components:
- Task Queue: An MCP server that manages pending, in-progress, and completed tasks
- Cron Scheduler: Triggers worker execution at regular intervals
- Claude Worker: The main script that fetches tasks and executes them
- Feedback Loop: Updates task status and stores results back to the queue
Why This Approach?
Traditional automation tools are great for deterministic tasks, but they struggle with complex, context-dependent work. By leveraging Claude's reasoning capabilities in an automated framework, we can handle tasks that require:
- Code review and analysis
- Dynamic problem-solving
- Natural language processing
- Complex file manipulation
- Multi-step workflows with decision points
Setting Up the Worker Script
The heart of our system is a shell script that connects all the pieces. Here's our main worker:
#!/bin/bash
# claude-worker.sh
set -euo pipefail # Strict mode for error handling
LOG_FILE="/var/log/claude-worker.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
# Source user environment to get correct Node version and PATH
source_user_environment() {
[[ -f "$HOME/.zshrc" ]] && source "$HOME/.zshrc"
[[ -f "$HOME/.zshenv" ]] && source "$HOME/.zshenv"
# Load Node Version Manager if present
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
source "$HOME/.nvm/nvm.sh"
nvm use default || nvm use node
fi
}
main() {
log "Starting Claude worker"
# Load user environment
source_user_environment
# Verify environment
log "Using Node: $(which node) ($(node --version))"
# Execute Claude with structured prompt
claude -p "/process-next-task" \
--output-format=stream-json \
--verbose \
-dangerously-skip-permissions
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
log "Worker cycle completed successfully"
else
log "Worker cycle failed (exit code: $exit_code)"
fi
return $exit_code
}
main "$@"
Understanding set -euo pipefail
The set -euo pipefail line enables "strict mode" for bash, which is crucial for automated scripts:
-e: Exit immediately if any command fails-u: Treat undefined variables as errors-o pipefail: Make pipeline failures detectable
This prevents silent failures that could leave your automation in an inconsistent state.
Structured Prompt Architecture
Instead of embedding complex logic in shell scripts, we use Claude's structured prompt system. Each task type gets its own prompt template:
/opt/claude-prompts/
├── process-next-task.md
├── code-review-task.md
├── weather-report.md
├── data-analysis-task.md
└── file-processing-task.md
Master Task Processor
The /process-next-task prompt handles the main workflow:
# Process Next Task
You are an automated worker. Your job is to:
1. Connect to the task management MCP server
2. Fetch the next pending task
3. If no tasks available, exit gracefully
4. Mark the task as "in-progress"
5. Route to the appropriate task handler based on task type
6. Handle any errors by marking the task as "failed"
## Task Types and Routing
- `code-review` → Use `/code-review-task` prompt
- `weather-report` → Use `/weather-report` prompt
- `data-analysis` → Use `/data-analysis-task` prompt
- `file-processing` → Use `/file-processing-task` prompt
## Error Handling
If any step fails:
1. Mark the task as "failed" in the task management system
2. Log the error details
3. Exit with appropriate error code
Begin processing now.
Task-Specific Prompts
Each task type has a dedicated prompt that handles the complete lifecycle:
# Weather Report Task
You are executing an automated weather report task.
## Instructions
1. Fetch task details from the task management system
2. Generate the requested weather report using web search
3. Save the report to files in the working directory
4. Upload results to the task management system
5. Mark task as "completed" with a summary
## Available Tools
- Task management MCP server for status updates
- Web search for current weather data
- File system for saving outputs
Execute the weather report now.
Cron Configuration
Schedule your worker to run every 10 minutes:
# Add to crontab -e
SHELL=/bin/zsh
PATH=/usr/local/bin:/usr/bin:/bin
# Run every 10 minutes with full environment
*/10 * * * * /bin/zsh -l -c '/path/to/claude-worker.sh' >> /var/log/claude-worker-cron.log 2>&1
The -l flag ensures cron uses a login shell, preserving your full environment including Node.js versions and PATH settings.
MCP Server Integration
Your MCP server should provide these essential functions:
get_next_task()- Fetch the next pending taskupdate_task_status(id, status)- Update task progresscomplete_task(id, results)- Mark task complete with resultsfail_task(id, error)- Mark task as failed with error details
Task Management Workflow
graph TD
A[Cron Triggers] --> B[Worker Starts]
B --> C[Get Next Task]
C --> D{Task Available?}
D -->|No| E[Exit Gracefully]
D -->|Yes| F[Mark In Progress]
F --> G[Route by Task Type]
G --> H[Execute Task]
H --> I{Success?}
I -->|Yes| J[Mark Complete]
I -->|No| K[Mark Failed]
J --> L[Log Results]
K --> L
L --> M[Worker Ends]
Error Handling and Monitoring
Robust error handling is essential for unattended operation:
# Add cleanup trap for unexpected exits
cleanup() {
if [[ -n "${TASK_ID:-}" ]]; then
log "Emergency cleanup for task $TASK_ID"
claude -p "/task-failure" \
--task-id="$TASK_ID" \
--error="Worker interrupted" \
--output-format=stream-json \
-dangerously-skip-permissions >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT
Benefits of This Approach
Autonomous Operation: Once configured, the system runs without human intervention, processing tasks as they appear in the queue.
Scalable: Add new task types by creating new prompt templates. No code changes required.
Reliable: Strict error handling and comprehensive logging ensure problems are caught and reported.
Flexible: Tasks can be anything Claude can handle - from code analysis to report generation to data processing.
Observable: Stream JSON output and comprehensive logging provide full visibility into operations.
Use Cases
This automated worker pattern is perfect for:
- Continuous Code Review: Automatically review pull requests
- Data Processing: Process uploaded files and generate reports
- Content Generation: Create documentation, summaries, or articles
- System Monitoring: Analyze logs and generate alerts
- Research Tasks: Gather and synthesize information from multiple sources
Production Considerations
Security: Use -dangerously-skip-permissions carefully and ensure your MCP servers have proper authentication.
Resource Limits: Consider task timeouts and memory limits for long-running operations.
Monitoring: Set up alerts for worker failures and task queue backlogs.
Backup Strategy: Ensure task results are properly persisted and backed up.
Conclusion
Automated Claude Code workers represent a new paradigm in automation - combining the reliability of traditional scripts with the intelligence and flexibility of AI reasoning. By structuring your automation around task queues, structured prompts, and robust error handling, you can build systems that handle complex, context-dependent work autonomously.
The key insight is letting Claude manage its own task lifecycle through structured prompts, while keeping the shell wrapper minimal and focused on environment setup and scheduling. This creates a clean separation of concerns and makes the system easier to debug and extend.
Start with simple tasks like report generation or file processing, then gradually expand to more complex workflows as you gain confidence in the system. The combination of cron scheduling, MCP server integration, and Claude's reasoning capabilities opens up entirely new possibilities for intelligent automation.
Have you built automated AI workers? Share your experiences and use cases in the comments below.