Skip to content

Claude Code Automated Execution - Complete cron Configuration Guide

Key Points

  • Scheduled Automatic Execution

    Claude Code automatically executes article creation and updates at specified times

  • News Article Automation

    Complete automation from searching for latest news to creating and publishing articles

  • Log Monitoring

    Monitoring execution status and errors with log rotation functionality

  • Git Integration

    Complete workflow from article creation to automatic commit and push for site updates

📖 Overview

By setting up Claude Code automated execution with cron, you can completely automate the creation, updating, and publishing of regular news articles. This covers everything from cron configuration in WSL environments to error handling and log management.

Benefits of Automation

  • Continuous Content Generation: Regular article updates without manual intervention
  • Automatic Latest Information Capture: Automatically search for news within the last 12 hours
  • Complete Workflow: End-to-end automation from article creation to site publication

🔧 Setup Procedure

1. Verify Script Placement

# Check already created script files
ls -la "$HOME"/note/scripts/

The following files are placed:

scripts/
├── auto-claude-news.sh      # Main execution script
├── log-monitor.sh           # Log monitoring and management script
└── crontab-sample.txt       # Cron configuration sample

2. Implement cron Configuration

Enable cron in WSL

# Start cron service (WSL environment)
sudo service cron start

# Set up automatic startup
sudo systemctl enable cron

crontab Configuration Steps

# Backup current configuration
crontab -l > crontab_backup.txt

# Edit crontab
crontab -e
# Execute twice daily at 6 AM and 6 PM
# Example execution (adjust to user's home directory)
0 6,18 * * * $HOME/note/scripts/auto-claude-news.sh

# Other option examples:

# Execute every hour (be careful of API rate limits)
# 0 * * * * $HOME/note/scripts/auto-claude-news.sh

# Weekdays only at 9 AM
# 0 9 * * 1-5 $HOME/note/scripts/auto-claude-news.sh

# Once a week Saturday at 10 AM
# 0 10 * * 6 $HOME/note/scripts/auto-claude-news.sh

3. Check Environment Variables

# Check API key configuration
echo $ANTHROPIC_API_KEY | head -c 20

# Check if environment variables are set in .bashrc
grep ANTHROPIC ~/.bashrc

Important: API Key Configuration

If ANTHROPICAPIKEY is not set, add the following to bashrc:

export ANTHROPIC_API_KEY="your-api-key-here"

📊 Monitoring and Operations

Log Monitoring Commands

# Comprehensive status check
./scripts/log-monitor.sh all

# Show latest logs only
./scripts/log-monitor.sh log

# Show error logs only
./scripts/log-monitor.sh errors

# Check running processes
./scripts/log-monitor.sh status

# Execute log rotation
./scripts/log-monitor.sh rotate

Log File Location

logs/
└── auto-claude-news.log    # Main execution log

Check Execution Status

# Check cron execution status
crontab -l

# Check running Claude processes
ps aux | grep claude

# Check recent execution results
tail -50 "$HOME"/note/logs/auto-claude-news.log

🛠️ Troubleshooting

Common Issues and Solutions

1. Environment Variables Not Loaded

# Check bashrc loading
source ~/.bashrc

# Check if environment variables are visible from cron
* * * * * env > /tmp/cron-env.txt

2. nvm Command Not Found

nvm path is explicitly specified in the script:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

3. Git Authentication Error

# Check SSH key configuration
ssh -T git@github.com

# For HTTPS authentication, check Personal Access Token configuration
git config --global credential.helper store

4. API Limit Error

# Adjust execution frequency
# Reduce frequency: every hour → twice daily → once weekly

# 0 6,18 * * * # Twice daily (recommended)
# 0 10 * * 6   # Once weekly

📋 Maintenance Tasks

# 1. Check log file size and rotate
./scripts/log-monitor.sh rotate

# 2. Check error logs
./scripts/log-monitor.sh errors

# 3. Check cron job execution history
grep CRON /var/log/syslog | grep claude

# 4. Check disk usage
du -sh "$HOME"/note/logs/

Update Tasks

# Update Claude Code version
npm update -g claude-code

# Update scripts (as needed)
git pull origin master

💡 Advanced Configuration Examples

Conditional Execution

For execution only under specific conditions:

# Add conditional branching to script
if [ "$(date +%u)" -eq 6 ]; then  # Saturday only
    claude --dangerously-skip-permissions "..."
fi

Multiple Article Type Automation

# Morning: Claude Code news, Evening: Generative AI news
0 6 * * * $HOME/note/scripts/auto-claude-news.sh
0 18 * * * $HOME/note/scripts/auto-ai-news.sh

Slack Notification Integration

# Notify Slack upon completion
curl -X POST -H 'Content-type: application/json' \
  --data '{"text":"Claude Code auto-execution completed"}' \
  YOUR_SLACK_WEBHOOK_URL

🚀 Execution Examples

Manual Test Execution

# Check script operation
./scripts/auto-claude-news.sh

# Background execution
nohup ./scripts/auto-claude-news.sh &

# Real-time log monitoring
tail -f "$HOME"/note/logs/auto-claude-news.log

Verify Setup Completion

# Check cron configuration
crontab -l

# Check next execution time
echo "Next scheduled execution:"
echo "Morning 6 AM: $(date -d 'tomorrow 06:00')"
echo "Evening 6 PM: $(date -d 'today 18:00')"

Automation Complete

With this configuration, article creation by Claude Code is fully automated. Maintain the continuous content update system through regular log checks and schedule adjustments as needed.