---
title: "Ping CronMissed from a Go program"
url: "https://cronmissed.com/snippets/go.md"
canonical_url: "https://cronmissed.com/snippets/go"
last_updated: "2026-10-22"
type: "how-to"
summary: "A small Go helper that reports start, success and failure to CronMissed with net/http, a client timeout and the tail of the error as the body."
related: ["https://cronmissed.com/snippets.md", "https://cronmissed.com/snippets/shell.md", "https://cronmissed.com/snippets/docker.md", "https://cronmissed.com/learn/exit-codes-and-failure-signals.md", "https://cronmissed.com/guides/kubernetes-cronjob.md"]
---

# Ping CronMissed from a Go program

> **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 small Go helper that reports start, success and failure to CronMissed with net/http, a client timeout and the tail of the error as the body.

Go's net/http is all a ping needs. This helper builds a client with a timeout, drains and closes every response so connections are reused, and wraps your job in the start, success and failure signals.

## What should a Go 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 helper?

```
package monitor

import (
	"context"
	"io"
	"net/http"
	"strings"
	"time"
)

var client = &http.Client{Timeout: 10 * time.Second}

// Ping sends one signal. suffix is "", "/start", "/fail" or "/<exit status>".
func Ping(ctx context.Context, base, suffix, body string) {
	method := http.MethodGet
	if body != "" {
		method = http.MethodPost
	}
	req, err := http.NewRequestWithContext(ctx, method, base+suffix, strings.NewReader(body))
	if err != nil {
		return
	}
	resp, err := client.Do(req)
	if err != nil {
		return // a lost ping must never break the job
	}
	defer resp.Body.Close()
	_, _ = io.Copy(io.Discard, resp.Body)
}

// Run reports a job's start, then its success, or its failure with the end of the error text.
func Run(ctx context.Context, base string, job func(context.Context) error) error {
	Ping(ctx, base, "/start", "")
	if err := job(ctx); err != nil {
		text := err.Error()
		if len(text) > 100000 {
			text = text[len(text)-100000:]
		}
		Ping(ctx, base, "/fail", text)
		return err
	}
	Ping(ctx, base, "", "")
	return nil
}
```

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.

## Questions

### Why drain the response body?

An unread body keeps the connection from being reused. For a job that pings twice, it makes little difference, but the habit stops leaks in long-running programs.

### What about a program that panics?

Recover in the caller, send /fail with the panic text, then re-panic or exit non-zero. A program killed outright sends nothing, which the check reports as a missed run.

## Related pages

- [Shell and curl](https://cronmissed.com/snippets/shell.md)
- [Docker](https://cronmissed.com/snippets/docker.md)
- [Exit codes and failure signals](https://cronmissed.com/learn/exit-codes-and-failure-signals.md)
- [Kubernetes CronJobs](https://cronmissed.com/guides/kubernetes-cronjob.md)

## Related

- [Ping snippets for 12 languages and tools](https://cronmissed.com/snippets.md)
- [Ping CronMissed from a shell script with curl](https://cronmissed.com/snippets/shell.md)
- [Ping CronMissed from a Docker container](https://cronmissed.com/snippets/docker.md)
- [Exit codes and failure signals in cron jobs](https://cronmissed.com/learn/exit-codes-and-failure-signals.md)
- [Kubernetes CronJob schedule: monitor every run](https://cronmissed.com/guides/kubernetes-cronjob.md)
