Simplify your cron job schedules with buckette
Most of the posts here are about agents. This one is about the oldest automated spender there is: the cron job. A nightly report, a backfill, an export to a partner, an agent run that summarises yesterday’s tickets. Each one starts with a line like this:
0 2 * * * /opt/jobs/nightly-report
One line is easy. The trouble starts around it:
- Two hosts, two runs. You put the line on a second machine so the
job survives one of them going down, and now it runs twice.
flockonly stops overlaps on one machine. - A failed run waits a day. The job failed at 02:00 because a dependency was down. Nothing tries again until tomorrow, unless someone logs in.
- A run that is still going. Last night’s run is slow, tonight’s starts on top of it.
- “Run it once more today” means a shell on a production host.
- Moving it to 03:00, or to every six hours, is a change to every crontab, deployed.
- The clock change. In the spring, 02:00 does not exist in Paris; depending on your cron, a 02:00 job runs late or not at all that night.
All of these come from one design choice: the crontab decides both when to try and whether this try is the run. Split the two. Let cron fire often, on every host, and ask buckette whether this firing gets the turn.
1. The turn is a bucket
A bucket holds one dimension, runs, with room for one, refilled once a
day. The bucket also gets a reservation policy, so that a hold nobody
settles frees itself after two hours (set it above your longest run).
POST /buckets
{"cbrResources": {"runs": {"raCurrent": 1, "raMaximum": 1, "raReserved": 0,
"raUnit": "run", "raMode": "Locking"}},
"cbrMetadata": {"buckette/bucket-title": "nightly-report: one run a day"},
"cbrReservationPolicy": {"default_ttl_seconds": 7200, "max_ttl_seconds": 7200}}
PUT /buckets/<bucket>/replenish-config
{"rcSchedule": {"tag": "Daily", "at": "02:00", "zone": "Europe/Paris"},
"rcAmounts": {"runs": 1},
"rcCarryover": "CarryoverNone", "rcOverflow": "OverflowDiscard"}
GET /buckets/<bucket>/replenish-config reads the schedule back in words,
with the next run: "every day at 02:00 Europe/Paris", next at
2026-12-06T01:00:00Z. Because the bucket holds at most one run and
nothing carries over, a job whose hosts were all down for three days gets
one run when they come back, not three.
The hosts get a key that can only spend from this bucket (POST /api-keys
with "cakreScopes": ["ScopeConsume"] and "cakreBucketId" set to it).
2. Cron fires often; a small guard decides
Every host gets the same line, firing every ten minutes:
*/10 * * * * /opt/jobs/guarded <bucket> /opt/jobs/nightly-report
guarded asks for the turn by putting a hold on the run. If it gets the
hold, it runs the job, then commits the hold when the job succeeded (the
day’s run is spent) or releases it when the job failed (the run is back,
and the next firing, ten minutes later on whichever host, tries again).
If it does not get the hold, this firing was not the run, and it exits
quietly.
#!/usr/bin/env bash
# guarded <bucket-id> <command> [args...]
# Runs the command only if this firing gets the bucket's turn.
set -u
bucket=$1; shift
api() { curl -sS -X POST "$BUCKETTE_URL$1" -H "Authorization: Bearer $BUCKETTE_KEY" \
-H 'Content-Type: application/json' -d "$2"; }
answer=$(api "/buckets/$bucket/reservations" \
"{\"rrvAmounts\": {\"runs\": 1}, \"rrvMode\": {\"tag\": \"Strict\"},
\"rrvMetadata\": {\"host\": \"$(hostname)\"}}") || exit 1
case $(jq -r '.tag // .errCode' <<<"$answer") in
ReserveSuccess) ;; # our turn
ReserveShortfall|depleted|bucket_not_active) exit 0 ;; # not this firing's
*) echo "guarded: $answer" >&2; exit 1 ;; # anything else is loud
esac
hold="/buckets/$bucket/reservations/$(jq -r .rsReservation.rvId <<<"$answer")"
if "$@"; then
api "$hold/commit" "{\"crqMetadata\": {\"buckette/title\": \"$1 on $(hostname)\"}}" >/dev/null
else
status=$?
api "$hold/release" "{\"rlrOutcome\": \"failed\", \"rlrMetadata\": {\"exit\": $status}}" >/dev/null
exit $status
fiThree answers mean “not your turn”, and the script treats them alike:
ReserveShortfall (another host holds the run right now), depleted
(today’s run is done; the bucket is empty until the next refill) and
bucket_not_active (someone froze the job, below). Anything else, a
network error or a key that stopped working, does not run the job and
exits with an error, so cron’s mail tells you. A guard that runs the job
anyway when it cannot ask would bring back every double run it exists to
prevent.
What the crontab problems above turn into:
- Two hosts, two runs: one hold per run, so one host runs and the others are told no. Add a third host by copying the line.
- A failed run releases its hold with the outcome
failed, and the next firing runs it again, ten minutes later, not tomorrow. - A run that is still going holds the turn, so the firings during it are told no. A host that dies mid-run stops nothing for long: its hold expires under the reservation policy, and another host takes the turn.
- “Run it once more today” is a call, with a key that can refill this
bucket:
POST /buckets/<bucket>/replenishwith{"repAmounts": {"runs": 1}, "repSource": "manual"}. The next firing takes it. - Moving it is one
PUTof the schedule:{"tag": "Daily", "at": "03:00", ...}, or{"tag": "Every", "count": 6, "unit": "hours", "anchor": "2026-12-05T00:00:00Z"}with"runs": 1per dose. No crontab changes. - The clock change is the schedule’s: it fires a local time that the clock skips just after the gap, and a time that happens twice only once.
- Pausing it during a migration:
PATCH /buckets/<bucket>with{"pbrStatus": "Frozen", "pbrStatusReason": "paused during the migration"}. Every firing is told no, and the scheduled refill is skipped while the bucket is frozen.{"pbrStatus": "Active"}resumes it.
The bucket’s Activity in the Console is the job’s history: every hold with the host that took it, every failed run with its exit status, every successful one under the title the guard gave it.
The cost is the cadence: with a firing every ten minutes, the run starts up to ten minutes after the refill. Fire every minute if that matters; a firing that is told no is one small request.
3. Advanced: start it from a webhook
Polling has two limits. It starts the run somewhere in the ten minutes after the refill. And it only knows about time: some jobs should run when something happens, such as the night’s export landing in a bucket of files, and at most once per export.
buckette can tell you instead. A webhook subscription to
bucket.replenished posts to your service the moment the bucket gets its
run back:
POST /webhooks
{"cwrUrl": "https://jobs.example.com/hooks/buckette",
"cwrEvents": ["bucket.replenished"],
"cwrSecret": "<a secret for the signature>"}
The body carries event, bucket_id, the bucket’s metadata and, in
details, the new balance and the next scheduled refill. Your receiver
checks the signature (X-Buckette-Signature, an HMAC-SHA256 of the body
with your secret), looks up the job by bucket_id, answers 204 at once,
and runs the same guarded script in the background. The webhook says
when to try; the hold still decides which try is the run. A subscription
covers every bucket of the tenant, which is why the receiver looks the
job up by bucket_id.
Now the trigger can be anything that refills the bucket:
- The schedule. A scheduled refill is posted from the scheduler’s pass, which runs every 30 seconds, so the run starts within a minute of 02:00 instead of within ten.
- An event. Drop the schedule, and have the exporter refill the bucket when the export is complete, with a key that can only refill it:
POST /buckets/<bucket>/replenish
{"repAmounts": {"runs": 1}, "repSource": "exporter",
"repTransactionId": "export-2026-12-05"}
repTransactionId makes the refill idempotent: an exporter that announces
the same export twice (a retry, a re-run) adds nothing the second time and
sends no second webhook. So the job runs once per export, never two at
once, and never before the export exists.
Keep the cron line, at a slower pace (hourly is plenty). It is the safety
net, for three cases. A failed run releases its hold, and nothing refills
the bucket, so nothing is posted. A refill that finds the bucket still
holding its run (yesterday’s was never taken) changes no balance, and
buckette posts bucket.replenished only when the balance went up. And a
delivery can be lost: buckette retries one that gets no answer, a 5xx or
a 429, eight attempts over about an hour, then gives up. In all three the
hourly firing finds the run and takes it, and the guard makes the double
trigger harmless.
What a webhook needs that a cron line does not: the URL must be https
on a public address, its host on your tenant’s callback allowlist, and
covered by a destination you have verified. The
guide for agents has the
steps, under “Outbound destination validation”.
What you end up with
The crontab no longer knows the schedule. It knows only “try this job every ten minutes” (or every hour, next to a webhook), which is the same line on every host and never changes. The schedule, the “once”, the “not twice at once”, the pause and the extra run live in one place, where they can be read, changed and audited without a deploy.
Every request in this post was run against buckette before it was written. The getting started page creates a first bucket in a few minutes, and the guide for agents has every route used here: “Reserve vs. consume”, “Replenishment” and “Freezing a bucket” for the guard, “Async consumption — wiring the callback” for webhooks and their retries.
If you run a job this way, or one that would not fit it, write to hello@buckette.co.