AI writes code fast. It also writes confident-sounding code that fails silently at 9am. Here's the system to catch those failures before they cost you time.
You asked Claude Code to set up a cron job â a scheduled task to run a Python script and post to X. It generated code. It looked right. It didn't work. That's not a you problem. That's the default AI failure mode. AI writes for an ideal environment. Your server is real. This guide bridges that gap.
Before you ask Claude to write any code, answer these 5 questions. Open a new Claude chat and paste this template:
I need you to build a script. Before writing any code, confirm you understand these requirements: ENVIRONMENT: - OS: [Mac / Linux Ubuntu / Windows] - Python version: [run: python3 --version and paste result] - Full Python path: [run: which python3 and paste result] - Virtual environment: [yes/no â did you use pip to install tweepy?] - Where the script will live: [e.g. /home/joe/scripts/] TASK: - Posts a tweet to X (Twitter) on a schedule - Schedule: [every day at 9am / every hour / etc.] CREDENTIALS: - X API credentials stored in a .env file (NOT in the code) - Full absolute path to .env: [e.g. /home/joe/scripts/.env] - X app has write permissions enabled: [yes/no â check at developer.twitter.com] OUTPUT: - Log file at: [e.g. /home/joe/scripts/logs/post.log] - Log must capture: start time, success/failure, error message ON FAILURE: - Should it: log and exit / retry once / send me a notification? STOP before writing code. Tell me: 1. What assumptions you're still making 2. What I need to verify before we start 3. What could break in MY specific environment
python3 --version)which python3 and have the full pathOnce Claude gives you a script, open a brand new chat. Paste the code and this prompt. The "new chat" part matters â fresh context means no attachment to the output.
You are a code critic. NOT a code writer. This script is meant to run via cron and post to X/Twitter. Find every way this FAILS. Check specifically: 1. Will it work when cron runs it with no environment loaded? 2. Are ALL file paths absolute (starting with /)? 3. Is the .env file loaded with an absolute path? 4. Is auth validated BEFORE attempting to post? 5. Does it log errors or crash silently? 6. Does it use the correct Python path for this user's setup? 7. What happens when it fails â does it notify anyone? For each issue: exact line number and the fix. [PASTE CODE HERE]
What to look for: The critic should find at least 2â3 issues on any first draft. If it says "looks fine" â push back: "What would break if zero environment variables were loaded?"
Every auto-scheduled script needs this wrapper. Ask Claude to add it if you're not comfortable editing directly.
import logging
import sys
from dotenv import load_dotenv
# ââ LOAD ENV VARS â USE ABSOLUTE PATH ââ
load_dotenv('/home/joe/scripts/.env') # â change this
# ââ LOGGING â USE ABSOLUTE PATH ââ
logging.basicConfig(
filename='/home/joe/scripts/logs/post.log', # â change this
level=logging.DEBUG,
format='%(asctime)s | %(levelname)s | %(message)s'
)
logging.info("=== Script started ===")
try:
# ââ YOUR CODE GOES HERE ââ
logging.info("Post successful")
except Exception as e:
logging.error(f"FAILED: {e}")
sys.exit(1) # tells cron the script failed
Two paths to change: the .env path and the .log path. Both must be absolute â starting from /, not ./ or ~/. Not sure of your path? Run pwd in the folder where your script lives.
.env to your .gitignore file. Otherwise your API keys go public. This is the second half of the "credentials in .env" rule that most people miss until it's too late.mkdir -p /path/to/logs)Find the correct Python path first â this is critical if you installed tweepy via pip (virtual environment):
which python3
Use that exact path in both the simulation command and in your cron job. Then simulate how cron will run your script:
# Replace with the path from 'which python3' above env -i /usr/bin/python3 /absolute/path/to/your_script.py # If you used a virtual environment (pip install tweepy), use: env -i /home/joe/myproject/venv/bin/python3 /absolute/path/to/your_script.py
Check the log immediately after:
tail -f /absolute/path/to/logs/post.log
If this command fails but python3 script.py works normally â you found the environment gap. Paste the log error into Claude and ask for the fix.
crontab -e works on older macOS but Apple has progressively restricted cron. If your cron job won't run after setup, you may need to use launchd instead. Ask Claude: "Convert this cron job to a macOS launchd plist file" and it will handle it.which python3 and have my exact Python pathBefore the cron job runs for real, write down what you expect. Literally write it down:
SMOKE TEST â [Date] [Time] EXPECTED IMMEDIATE: - Script runs at [time] - Log shows: "=== Script started ===" then "Post successful" - Tweet appears on X within 2 minutes - No error lines in the log EXPECTED 2ND ORDER (still true 24 hrs later): - No duplicate posts - Auth still valid (no expired token errors in log) - Log file under 1MB - Script runs again at next scheduled time without manual restart ACTUAL (fill in after it runs): - Log result: - Tweet appeared: YES / NO - Any errors:
Simulation passed. Now set the real cron job. Open terminal:
crontab -e
Add your schedule. Format: minute hour * * * /full/path/to/python3 /full/path/to/script.py
# Every day at 9:00 AM â use the python path from 'which python3' 0 9 * * * /usr/bin/python3 /home/joe/scripts/post.py # Every hour 0 * * * * /usr/bin/python3 /home/joe/scripts/post.py # Weekdays at 8:30 AM 30 8 * * 1-5 /usr/bin/python3 /home/joe/scripts/post.py
When the first run happens, check the log and compare to your smoke test. If it fails:
Here is my error log. Here is the script. Tell me: 1. The exact line that failed 2. Why it failed 3. The corrected script only â no explanation needed LOG: [paste log here] SCRIPT: [paste script here]