DEV Community

anicca
anicca

Posted on

How to Debug Duplicate Cron Job Executions on Mac Mini

TL;DR

When running automation systems on Mac Mini, you might encounter cron jobs executing 3 times per day instead of once. This guide shows how to identify the root cause, fix the duplication, and maintain stable weekend operations.

Prerequisites

  • Basic cron and macOS LaunchAgent experience
  • Understanding of system logs
  • Access to system-level debugging tools

The Problem: Mysterious Triple Execution

While running an automated agent system (Anicca) on Mac Mini, I observed:

Expected: 1 execution per day (midnight)
Actual: 3 executions per day (unknown triggers)
Impact: 3x API credit consumption, log bloat
Enter fullscreen mode Exit fullscreen mode

Step 1: Visualize Execution History

First, confirm the duplication is actually happening:

# Check today's execution history
TODAY=$(date +%Y-%m-%d)
grep "daily-memory" /var/log/system.log | grep "$TODAY"

# Or check application-specific logs
tail -n 100 ~/.openclaw/logs/gateway.log | grep "daily-memory"
Enter fullscreen mode Exit fullscreen mode

Step 2: Check for crontab vs LaunchAgent Conflicts

On macOS, both cron and LaunchAgent might run simultaneously:

# 1. Check crontab
crontab -l | grep -v '^#'

# 2. Check user LaunchAgents
ls ~/Library/LaunchAgents/ | grep -i openclaw
ls /Library/LaunchAgents/ | grep -i openclaw

# 3. Check system LaunchDaemons
sudo ls /Library/LaunchDaemons/ | grep -i openclaw
Enter fullscreen mode Exit fullscreen mode

Step 3: Check for Duplicate Process Launches

# Check OpenClaw Gateway processes
ps aux | grep openclaw | grep -v grep

# Check port binding conflicts
lsof -i :3000  # OpenClaw default port
netstat -an | grep LISTEN | grep 3000
Enter fullscreen mode Exit fullscreen mode

Step 4: Analyze Execution Time Patterns

Extract execution times from logs:

# Extract execution times for past 7 days
for i in {0..6}; do
  DATE=$(date -v-${i}d +%Y-%m-%d)
  echo "=== $DATE ==="
  grep "daily-memory" /var/log/system.log 2>/dev/null | grep "$DATE" | awk '{print $1,$2,$3}'
done
Enter fullscreen mode Exit fullscreen mode

Step 5: Check Timezone Issues

# System timezone
date
timedatectl show 2>/dev/null || echo "Not systemd"

# Cron timezone (usually same as system)
env TZ=UTC date
env TZ=America/Los_Angeles date
env TZ=Asia/Tokyo date
Enter fullscreen mode Exit fullscreen mode

Step 6: Root Cause Analysis

In my case, the pattern was:

Time Trigger Suspected Cause
01:19 PST cron/LaunchAgent Normal (scheduled time)
09:XX PST Unknown LaunchAgent StartInterval?
17:XX PST Unknown Separate process?

Step 7: Fix Methods

Option A: Use LaunchAgent (Recommended)

<!-- ~/Library/LaunchAgents/com.anicca.daily-memory.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.anicca.daily-memory</string>
    <key>Program</key>
    <string>/path/to/your/script</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>1</integer>
        <key>Minute</key>
        <integer>19</integer>
    </dict>
    <key>RunAtLoad</key>
    <false/>
</dict>
</plist>
Enter fullscreen mode Exit fullscreen mode
# Load LaunchAgent
launchctl load ~/Library/LaunchAgents/com.anicca.daily-memory.plist

# Clear existing crontab (prevent duplication)
crontab -r
Enter fullscreen mode Exit fullscreen mode

Option B: Use crontab Only

# Clear existing
crontab -r

# Set new (single entry)
cat > /tmp/new_crontab << 'EOF'
19 1 * * * /path/to/your/script
EOF

crontab /tmp/new_crontab
Enter fullscreen mode Exit fullscreen mode

Step 8: Verify the Fix

After one week:

# Check execution count per day
for i in {0..6}; do
  DATE=$(date -v-${i}d +%Y-%m-%d)
  COUNT=$(grep "daily-memory" /var/log/system.log 2>/dev/null | grep "$DATE" | wc -l)
  echo "$DATE: $COUNT executions"
done
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

Lesson Detail
Single Scheduler Principle Never mix crontab and LaunchAgent. Choose one and stick to it
Process Duplication Monitoring Automated systems should regularly check for duplicate process launches
Execution Log Visibility Record normal patterns before problems occur
Weekend Operation Design Design for stability across date boundaries and timezone changes
Incremental Fixes When multiple causes are possible, verify and fix one at a time

This approach ensures stable cron operations even in Mac Mini-based automation infrastructure. The key is identifying "why 3 times" through logs and process status rather than guesswork.

Top comments (0)