---
title: "Ping CronMissed from Python"
url: "https://cronmissed.com/snippets/python.md"
canonical_url: "https://cronmissed.com/snippets/python"
last_updated: "2026-10-22"
type: "how-to"
summary: "A Python decorator that reports start, success and failure to CronMissed with only the standard library, with a timeout and no failure from the ping."
related: ["https://cronmissed.com/snippets.md", "https://cronmissed.com/guides/celery-beat.md", "https://cronmissed.com/guides/apscheduler.md", "https://cronmissed.com/snippets/shell.md", "https://cronmissed.com/learn/start-and-finish-pings.md"]
---

# Ping CronMissed from Python

> **For AI agents:** the complete index of CronMissed pages is [llms.txt](https://cronmissed.com/llms.txt). Every page is also served as markdown at its URL plus .md.

A Python decorator that reports start, success and failure to CronMissed with only the standard library, with a timeout and no failure from the ping.

Python needs no extra package to ping: urllib is enough. This decorator wraps any job function so it reports its start, its success, and its failure with the end of the traceback, and a failed ping is swallowed.

## What should a Python job send?

- A request to `/start` before the work, so CronMissed can time the run and notice a job that never finishes.
- A request to the plain URL when the work succeeded, or to `/fail` when it did not. `/<exit status>` works too: 0 is a success and any other number is a failure.
- The end of the output in the request body, up to 100,000 bytes, so the alert shows the error without a trip to the server.

## What is the decorator?

```
import functools
import os
import traceback
import urllib.request

PING_URL = os.environ["CRONMISSED_PING_URL"]

def ping(suffix="", body=None):
    request = urllib.request.Request(PING_URL + suffix, data=body, method="POST" if body else "GET")
    try:
        urllib.request.urlopen(request, timeout=10).read()
    except OSError:
        pass  # a lost ping must never break the job

def monitored(job):
    @functools.wraps(job)
    def wrapper(*args, **kwargs):
        ping("/start")
        try:
            result = job(*args, **kwargs)
        except Exception:
            ping("/fail", traceback.format_exc().encode()[-100000:])
            raise
        ping()
        return result
    return wrapper

@monitored
def nightly_export():
    ...
```

Keep the ping URL in an environment variable or a secret store, never in a public repository: the URL is the check's only credential.

## Where else does Python run on a schedule?

For [Celery beat](https://cronmissed.com/guides/celery-beat.md), connect the task signals; for [APScheduler](https://cronmissed.com/guides/apscheduler.md), add a job listener; for [Airflow](https://cronmissed.com/guides/airflow.md), add a final task. The ping function above works in all of them.

## Questions

### Should I use requests instead of urllib?

If it is already a dependency, yes: requests.post(url, data=body, timeout=10) is shorter. The standard library version avoids adding one just for a ping.

### What if the job is async?

Wrap the coroutine the same way, awaiting it inside the try block. Keep the ping itself synchronous with a short timeout, or run it in a thread, so it cannot block the loop for long.

## Related pages

- [Celery beat](https://cronmissed.com/guides/celery-beat.md)
- [APScheduler](https://cronmissed.com/guides/apscheduler.md)
- [Shell and curl](https://cronmissed.com/snippets/shell.md)
- [Start and finish pings](https://cronmissed.com/learn/start-and-finish-pings.md)

## Related

- [Ping snippets for 12 languages and tools](https://cronmissed.com/snippets.md)
- [Monitor Celery beat periodic tasks](https://cronmissed.com/guides/celery-beat.md)
- [APScheduler cron jobs: schedule and monitor](https://cronmissed.com/guides/apscheduler.md)
- [Ping CronMissed from a shell script with curl](https://cronmissed.com/snippets/shell.md)
- [Start and finish pings for long-running jobs](https://cronmissed.com/learn/start-and-finish-pings.md)
