Code examples
Replace PING_KEY with the key of your monitor. The host is api.cronbeat.dev.
crontab
Section titled “crontab”0 2 * * * /scripts/backup.sh && curl -fsS -m 10 --retry 3 https://api.cronbeat.dev/ping/PING_KEY > /dev/nullBash with start and fail
Section titled “Bash with start and fail”curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY/start > /dev/nullif /scripts/backup.sh; then curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY > /dev/nullelse curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY/fail > /dev/nullfiHost agent
Section titled “Host agent”curl -fsSL https://docs.cronbeat.dev/install.sh | sh # once per servercronbeat configure # paste the key of a Linux server agent, once per user0 2 * * * /usr/local/bin/cronbeat run --name nightly-backup --schedule '0 2 * * *' --grace 15m -- /scripts/backup.shThe exit code of /scripts/backup.sh is passed through unchanged. cronbeat crontab apply wraps all jobs of a crontab
for you, and systemd timers and Docker work the same way, see The run command and
systemd timers and Docker.
Kubernetes CronJob (manual method)
Section titled “Kubernetes CronJob (manual method)”The container image must contain curl and a shell. Replace the image and the command with your own.
apiVersion: batch/v1kind: CronJobmetadata: name: backupspec: schedule: "0 2 * * *" jobTemplate: spec: template: spec: restartPolicy: Never containers: - name: backup image: your-image-with-curl:tag command: - /bin/sh - -c - | PING=https://api.cronbeat.dev/ping/PING_KEY curl -fsS -m 10 "$PING/start" > /dev/null if /scripts/backup.sh; then curl -fsS -m 10 "$PING" > /dev/null else curl -fsS -m 10 "$PING/fail" > /dev/null exit 1 fiKeep the ping key in a Secret and expose it as an environment variable if the manifest is stored in a public repository.
Reporting a number (curl, Python, Bash)
Section titled “Reporting a number (curl, Python, Bash)”Add the numbers your job produced as m_<name> parameters. With a
success-run check such as rows_processed > 0, a run that “succeeds” with zero
rows is treated as failed.
ROWS=$(/scripts/import.sh) # prints the number of imported rowscurl -fsS -m 10 "https://api.cronbeat.dev/ping/PING_KEY?m_rows_processed=$ROWS" > /dev/nullimport requests
rows = run_import()requests.get("https://api.cronbeat.dev/ping/PING_KEY", params={"m_rows_processed": rows}, timeout=10)GitHub Actions
Section titled “GitHub Actions”Add the ping as the last step. if: failure() makes the failure step run only when an earlier step failed.
name: nightlyon: schedule: - cron: "0 2 * * *"jobs: report: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: ./scripts/report.sh - name: CronBeat success run: curl -fsS -m 10 --retry 3 https://api.cronbeat.dev/ping/PING_KEY - name: CronBeat fail if: failure() run: curl -fsS -m 10 --retry 3 https://api.cronbeat.dev/ping/PING_KEY/failStore the key as a repository secret (for example CRONBEAT_PING_KEY) and use
https://api.cronbeat.dev/ping/${{ secrets.CRONBEAT_PING_KEY }} if the repository is public.
Python, Java and Go
Section titled “Python, Java and Go”Python
Section titled “Python”import requests
PING = "https://api.cronbeat.dev/ping/PING_KEY"
requests.get(PING + "/start", timeout=10)try: run_job() requests.get(PING, timeout=10)except Exception: requests.post(PING + "/fail", data="job failed", timeout=10) raiseimport java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.time.Duration;
HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.cronbeat.dev/ping/PING_KEY")) .timeout(Duration.ofSeconds(10)) .GET() .build();client.send(request, HttpResponse.BodyHandlers.discarding());package main
import ( "net/http" "time")
func ping(suffix string) { client := &http.Client{Timeout: 10 * time.Second} resp, err := client.Get("https://api.cronbeat.dev/ping/PING_KEY" + suffix) if err == nil { resp.Body.Close() }}
func main() { ping("/start") // ... run the job ... ping("") // success; use ping("/fail") on error}
