How to Monitor Broken Affiliate Links with Python Scheduled Cron Jobs

In content publishing and media monetization, broken affiliate links are silent revenue killers. Merchant programs shut down, product URLs change without notice, and affiliate network tracking scripts break—turning high-converting articles into 404 dead ends. By writing an automated Python link auditing script running on a scheduled Linux cron job, publishers can detect dead redirects, landing page 404s, and merchant domain shifts immediately.

In this technical guide, we build an asynchronous link crawler in Python using httpx, inspect multi-hop redirect chains, bypass WAF false-positives with custom user agents, and dispatch real-time alerts to Slack or Telegram when broken links are detected.

Audit Phase Technical Action Error Classification
1. URL Extraction Pulls outbound links from WordPress REST API or local sitemap. Filters strictly outbound affiliate redirects (e.g. /go/ or partner domains).
2. Redirect Chain Traversal Follows 301/302 redirects up to 5 hops with TLS validation. Detects redirect loops, SSL certificate expirations, and broken domains.
3. Landing Page Status Evaluates final HTTP response code and body keywords. Flags 404 Not Found, 500 Server Error, or “Offer Expired” strings.
4. Alert Dispatch Sends Telegram / Slack notification with offending post URL. Includes exact line reference and proposed alternative merchant.

2. Production Python Link Crawler (monitor_links.py)

# monitor_links.py
import asyncio
import httpx
import os

TELEGRAM_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
TELEGRAM_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")

TARGET_AFFILIATE_LINKS = [
    "https://zelobiz.com/go/coolify",
    "https://zelobiz.com/go/hetzner",
    "https://zelobiz.com/go/supabase",
]

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}

async def check_link(client: httpx.AsyncClient, url: str):
    try:
        r = await client.get(url, headers=HEADERS, follow_redirects=True, timeout=12.0)
        
        # Check for HTTP error codes
        if r.status_code >= 400:
            return {"url": url, "error": f"HTTP {r.status_code}", "final_url": str(r.url)}
        
        # Check for soft 404 or expired deal keywords
        body_lower = r.text.lower()
        if "offer expired" in body_lower or "page not found" in body_lower:
            return {"url": url, "error": "Soft 404 / Offer Expired", "final_url": str(r.url)}

        return None
    except Exception as e:
        return {"url": url, "error": str(e), "final_url": "N/A"}

async def main():
    async with httpx.AsyncClient() as client:
        tasks = [check_link(client, url) for url in TARGET_AFFILIATE_LINKS]
        results = await asyncio.gather(*tasks)

        broken = [res for res in results if res is not None]
        if broken:
            msg = f"🚨 *AFFILIATE LINK AUDIT ALERT*\n\nFound {len(broken)} broken links:\n"
            for b in broken:
                msg += f"• `{b['url']}` -> *{b['error']}* (Target: {b['final_url']})\n"
            
            # Send Telegram alert
            if TELEGRAM_TOKEN and TELEGRAM_CHAT_ID:
                async with httpx.AsyncClient() as alert_client:
                    await alert_client.post(
                        f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage",
                        json={"chat_id": TELEGRAM_CHAT_ID, "text": msg, "parse_mode": "Markdown"}
                    )
            print(msg)
        else:
            print("✓ All affiliate links healthy.")

if __name__ == "__main__":
    asyncio.run(main())

3. Scheduled Linux Cron Configuration

Schedule the audit to run automatically every morning at 06:00 UTC:

# Run daily link health check and append results to audit log
0 6 * * * /usr/bin/python3 /var/scripts/monitor_links.py >> /var/log/affiliate_audit.log 2>&1

Frequently Asked Questions (FAQs)

Why do my crawler requests get blocked with 403 Forbidden?

Target merchants and affiliate networks use Web Application Firewalls (Cloudflare, Akamai) that block default Python requests and urllib user-agents. Always send legitimate modern browser User-Agent and Accept headers with a slight delay between concurrent batches.


Mamun Hossain

Mamun Hossain

Systems Architect

Founder of Zelobiz & Enterprise Infrastructure Architect. Specializing in autonomous software engineering workflows, server-side tracking pipelines, and high-performance self-hosted infrastructure.

✓ 100% Independent Technical Evaluation ✓ Benchmarked on Dedicated Hardware Follow Zelo Biz on Facebook ↗

Editorial Disclosure: Zelobiz publishes independent engineering benchmarks and architectural evaluations. We may receive affiliate compensation when you subscribe to partner hosting services through our links at no additional cost to you.