Skip to content

Code examples

Replace PING_KEY with the key of your monitor. The host is api.cronbeat.dev.

0 2 * * * /scripts/backup.sh && curl -fsS -m 10 --retry 3 https://api.cronbeat.dev/ping/PING_KEY > /dev/null
Terminal window
curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY/start > /dev/null
if /scripts/backup.sh; then
curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY > /dev/null
else
curl -fsS -m 10 https://api.cronbeat.dev/ping/PING_KEY/fail > /dev/null
fi
Terminal window
curl -fsSL https://docs.cronbeat.dev/install.sh | sh # once per server
cronbeat configure # paste the key of a Linux server agent, once per user
0 2 * * * /usr/local/bin/cronbeat run --name nightly-backup --schedule '0 2 * * *' --grace 15m -- /scripts/backup.sh

The 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.

The container image must contain curl and a shell. Replace the image and the command with your own.

apiVersion: batch/v1
kind: CronJob
metadata:
name: backup
spec:
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
fi

Keep the ping key in a Secret and expose it as an environment variable if the manifest is stored in a public repository.

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.

Terminal window
ROWS=$(/scripts/import.sh) # prints the number of imported rows
curl -fsS -m 10 "https://api.cronbeat.dev/ping/PING_KEY?m_rows_processed=$ROWS" > /dev/null
import requests
rows = run_import()
requests.get("https://api.cronbeat.dev/ping/PING_KEY", params={"m_rows_processed": rows}, timeout=10)

Add the ping as the last step. if: failure() makes the failure step run only when an earlier step failed.

name: nightly
on:
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/fail

Store 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.

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)
raise
import 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
}