kvbox.cloud
Guides

Cron

Call a box, or run a command in it, on a schedule. kvbox wakes the box first if it is hibernated.

A crontab inside a box only runs while the box is awake: a hibernated box sleeps through it. The cron service keeps the schedule outside the box. When a trigger is due, kvbox wakes the box if it has to and then does one of two things: sends an HTTP request to a port in it, so your app does the work when the request arrives, or runs a command in it.

kvbox cron create nightly                                    # a cron service: a named set of triggers
kvbox cron trigger add nightly api "0 6 * * *" --port 3000 --path /tasks/embed --tz Europe/Bucharest
kvbox cron trigger add nightly api "30 2 * * *" --run "node ~/app/cleanup.js"   # or run a command in the box
kvbox cron trigger run nightly <id>                          # fire it now, to try it
kvbox cron trigger runs nightly <id>                         # how the last runs ended

Services and triggers

A cron service is a named set of triggers. One is usually enough; make more to keep things apart (one per project, one per org).

A trigger is a schedule and what it does to a box:

  • the box: one of your boxes, or one of the org's when the service is the org's (--org). It can be on any host;
  • a request: the port your app listens on in the box (--port), the path and query to ask for (--path, / unless said) and the method (POST unless you say --method GET). The request has no body;
  • or a command to run in the box (--run "<command>"), see Run a command;
  • the schedule, in quotes: five fields, minute hour day-of-month month day-of-week. The shortest interval is one minute.
ExpressionFires
*/5 * * * *every five minutes
0 * * * *every hour, on the hour
0 6 * * *every day at 06:00
30 2 * * MON-FRIat 02:30, Monday to Friday
0 0 1 * *at midnight on the first of each month

The schedule is read in UTC unless you give a time zone: --tz Europe/Bucharest makes 0 6 * * * six in the morning there, summer and winter. When the clocks change, a time that happens twice fires once, and a time that is skipped fires when the clocks jump.

A trigger does not fire at the exact second: it is sent within a few seconds of its minute.

What your app gets

A plain HTTP request on the port you named, from inside kvbox's network, not from the internet: the port does not need an HTTPS address or to be published.

POST /tasks/embed HTTP/1.1
user-agent: kvbox-cron/0.6.1
content-length: 0

Answer with a 2xx status when the work is done, or when it has started. kvbox waits up to 55 seconds for the answer (plus the time the box takes to wake). For a job that runs longer, start it in the background and answer 202 at once.

In Node:

app.post("/tasks/embed", (req, res) => {
  res.status(202).end();
  embedAllDocs().catch((err) => console.error("embed failed", err));
});

A box set to hibernate when idle is judged by its network traffic, not by what runs in it: a long job that talks to nothing can be hibernated half-way, and goes on when the box next wakes. Give such a box a longer idle time, or none, or run the job as a command: a box is never hibernated for being idle while a trigger's command runs in it.

Run a command

kvbox cron trigger add nightly api "30 2 * * *" --run "node ~/app/cleanup.js"
kvbox cron trigger add nightly api "0 * * * *" --run "cd ~/app && ./sync.sh" --timeout 30m

With --run, nothing has to listen in the box: kvbox runs the command there when the trigger is due.

  • It runs as the box's user (kvbox), in its home, through a login shell, the way a command typed after kvbox connect would. The box's variables are set.
  • It is stopped after 10 minutes unless you say otherwise: --timeout 90s, 30m, up to 1h. Everything the command started is stopped with it.
  • One at a time: when a trigger's time comes and its previous command is still going, that time is skipped. Two copies of one trigger's command never run together.
  • The box is not hibernated for being idle while the command runs.
  • The run ends ok when the command exits with code 0 and failed with any other. The exit code is kept with the run.
  • What the command prints is not sent to kvbox. It stays in the box under /var/log/kvbox/runs/, two files per run (.out and .err; one .log on an image older than v0.16.0); the box keeps the newest 50 cron runs (commands run over the API are counted apart). kvbox cron trigger runs names the newest:
kvbox exec api -- cat /var/log/kvbox/runs/cron-812.*

The box must have been made from a current image: an older one cannot take commands, and the run says so (box image too old). A request to a port works on any box.

A box that is stopped, restarted or put to sleep while the command runs loses the run: it is recorded as lost.

Sleeping and stopped boxes

  • A hibernated or paused box is woken, then gets the request. The run says it woke the box.
  • A stopped box is left stopped: you stopped it on purpose. The run is recorded as skipped.
  • An account or org that is out of credit gets nothing sent: a trigger never puts a box back on the meter unpaid. The run is recorded as skipped.

Runs

kvbox cron trigger ls nightly            # each trigger: its schedule, what it does, the next run, how the last one ended
kvbox cron trigger runs nightly <id>     # the last runs, newest first
when     result  status  took    note
2m ago   ok      200     4.2 s   woke the box
1d ago   failed  500     80 ms   box api answered 500

For a command, the status is its exit code:

when     result  status  took     note
3h ago   ok      exit 0  95.0 s   woke the box
1d ago   failed  exit 3  42.0 s   the command ended with exit code 3; what it printed is in the box: /var/log/kvbox/runs/cron-812.*

A run keeps when it started, the status your app answered (or the command's exit code), how long it took and, when it failed, why in a line. kvbox keeps the last 100 runs of each trigger.

Price

A cron service costs $1 a month for as long as it exists, charged by the hour like everything else. The price is the same with one trigger or fifty, and whatever number of runs they make. kvbox cron rm stops the charge.

A box that a trigger wakes is back on the meter while it is awake, like a box woken any other way.

An account that runs out of credit keeps its cron services, and they keep costing; their triggers send nothing until there is credit again. Remove a service you no longer want.

kvbox usage lists each cron service with the hours it existed. The web app has the same under Usage, and the services themselves under Cron.

What your app prints is not kept: it is your app's own log, in the box.

ResultMeans
okyour app answered 2xx; a command exited with code 0
failedit answered something else, or nothing is listening on that port; a command exited with another code
timed outno answer in time; a command was stopped at its time limit
skipped: still runningnot started: this trigger's previous command had not ended
losthow it ended is not known: the box restarted or went quiet before the command ended
box image too oldthe box was made from an image that cannot take commands
skipped: no creditnot sent: the account may not spend
skipped: box stoppednot sent: the box is stopped
host unreachablethe server your box is on did not answer; the next run tries again
host needs an updatethe server your box is on is too old for the cron service

A failed run is not retried: the next time on the schedule is the retry.

Change and remove

kvbox cron trigger pause nightly <id>    # stop it firing; its runs are kept
kvbox cron trigger resume nightly <id>   # fire again from the next time on its schedule
kvbox cron trigger rm nightly <id>       # delete it and its runs
kvbox cron rm nightly                    # delete the service, its triggers and their runs

To change a trigger's schedule, request or command, remove it and add it again.

Removing a box removes the triggers that call it, and so does the box's host leaving kvbox: when the host comes back, its boxes do, their triggers do not. A box made from a snapshot or a fork starts with no triggers.

Limits

  • 20 cron services per account or org, 50 triggers in each.
  • One minute between runs at the least.
  • At most 20 runs of an account or org in flight at once; the next ones wait their turn.
  • A request: GET or POST, no body, no headers of your own.
  • A command: 4,096 bytes at most, one hour at most, one at a time per trigger.
  • Cloud only: on your own host's socket (--local), use the box's crontab.

On this page