Get an alert when a Python cron job or scheduled script fails
A Python script runs every night from cron or Windows Task Scheduler. When it raises an exception, the traceback goes to a log or a mailbox nobody reads, and you find out when the data is missing. A few lines in the script send that traceback to you instead, and a schedule catches the runs that hang or never start.
Python reports the failure; the scheduler keeps it to itself
An uncaught exception makes Python exit with status 1 (the Python documentation for sys.exit and the interpreter's main loop describe it: 0 is success, anything else is abnormal termination). So the scheduler does know. What it does with that:
- cron doesn't look at the exit status at all. It mails the job's output to the crontab's owner or
MAILTO, if the machine has a working mail program; cronie writes the output to syslog when it has none. A script that fails with output redirected to a file sends nothing anywhere. - Task Scheduler shows
0x1in the Last Run Result column, and that's it. Forum threads such as "How to fix (0x1) error in task scheduler that runs a python script" (Microsoft Q&A, 2023) start with someone noticing days later.
Some failures aren't exceptions at all: a try/except that logs and carries on, an API that returns an empty list, a query that finds no new rows. Python exits with 0 and the scheduler is satisfied. And a run that never started (machine off, cron daemon down, wrong path in the crontab) leaves no status of any kind.
Report from the script
The code below uses only the standard library (urllib), so it works in any Python 3 and any virtual environment. It pings TaskBeacon at the start; on an exception it sends /fail with the error and the last 20 lines of the traceback, then re-raises so the exit status stays 1; at the end it sends the row count.
# --- TaskBeacon: top of the script (standard library only) ---
import json, traceback, urllib.request
TB = "https://taskbeacon.io/ping/your-job-id"
def tb_ping(path="", data=None):
body = json.dumps(data).encode() if data else None
req = urllib.request.Request(TB + path, data=body, headers={"Content-Type": "application/json"})
try:
urllib.request.urlopen(req, timeout=10)
except Exception as e:
print("TaskBeacon ping failed:", e)
tb_ping("/start")
try:
# --- your code here ---
df = [1, 2, 3] # example data: replace with your own (e.g. pandas.read_csv)
except Exception as e:
# the error and the last 20 lines of the traceback go with the failure
tb_ping("/fail", {"error": f"{type(e).__name__}: {e}"[:200],
"log": "\n".join(traceback.format_exc().splitlines()[-20:])[-4000:]})
raise
# --- TaskBeacon: end of the script ---
tb_ping("", {"rows": len(df)})In the app, the Code tab of your job gives you this code with the job's own address instead of your-job-id, and you can untick the parts you don't need.
Replace the example line with your code and keep the result in df (a pandas DataFrame or a list of rows), or send another number. A failed ping prints a message and the script carries on: monitoring never breaks the job.
For a cron job, the crontab line stays as it is:
0 6 * * * /home/me/jobs/venv/bin/python /home/me/jobs/sales.py >> /home/me/jobs/sales.log 2>&1
If you'd rather not touch the script, the cron line reports the exit code instead (without the traceback or the row count).
What you get
- Failed with the exception and the end of the traceback, for example
KeyError: 'amount', in your email, Slack or Teams. - Missed when no ping arrived by the planned time plus grace: machine off, cron not running, a typo in the crontab.
- Late when the script started and is taking much longer than usual, such as a request without a timeout waiting forever.
- Odd data when the rows drop more than 40% or are 0.
Without TaskBeacon
On Linux, install and configure a mail program, set MAILTO, and let the script print only on errors, so cron mails you exactly then. On Windows, add a try/except at the top level that sends an email through smtplib before re-raising. Both cover exceptions. Neither covers a run that never started; for that you need something that expects a signal from the job, a heartbeat check, or a second job that checks the first one's output every morning.
Connected Claude or ChatGPT to TaskBeacon? Ask it "what failed last night, and why?" and it answers from your jobs. How that works.
Related
In the Docs: Python · Linux cron · Statuses.
- Heartbeat monitoring (a dead man's switch) for scheduled scriptsThe job checks in on every run; silence is the alert.
- Know when a scheduled R script failstaskscheduleR and cron write a log; nobody reads it until something is wrong.
- Get an alert when a job loads 0 rows, or far fewer than usualExit code 0 says nothing about how many rows arrived.
TaskBeacon is free to start, with every feature open for now and no credit card. Create a job, paste the code, and the first ping shows up on the job page.
Start free View live demoSources
- Python docs: Py_RunMain returns 1 for an unhandled exception (Python 3.15 docs, undated; checked 2026-11-09)
- Python docs: sys.exit (zero is success, non-zero abnormal termination) (Python 3.15 docs, undated; checked 2026-11-09)
- man7.org: crontab(5), cronie (MAILTO) (page 2012, matches upstream cronie 2026)
- man7.org: cron(8), cronie (output is mailed; without sendmail it goes to syslog) (page 2013, matches upstream cronie 2026)
- Microsoft Q&A: How to fix (0x1) error in task scheduler that runs a python script (2023-08-28, old: a sign of demand)
Page checked against its sources on 2026-11-09. Menus and limits of other products change; if something here no longer matches, write to support@taskbeacon.io.