dust

Cron

Introduction

dust's cron runs a dedicated daemon, dust-cron, which reads shell commands from the database and starts each due command in its own process. There is no system cron entry and no one-minute ceiling: expressions carry seconds.

Every run records its exit code, output, error, CPU time, and peak memory in cron_runs, so cron:list and cron:runs answer what ran, when, and with what result.

Setup

Enable the cron feature on dust-framework. A new application already does, and ships:

  • src/bin/dust-cron.rs, the daemon binary;
  • the crons, cron_runs, and cron_runners migrations;
  • storage/logs/cron.log and storage/framework/cron.pid, created on first use.

On an older application, add the feature, the binary, and the migration, then run dust migrate.

Build the daemon with the application:

shell
cargo build --bin dust-cron

dust build builds both binaries and copies them into bin/, which is where cron:start looks. dust update rebuilds both binaries as well.

dust install runs the pending migrations, adds the core schedule:run cron, which runs the application's scheduler every minute, and then starts the daemon. You may run the setup step on its own; it leaves an existing schedule:run cron alone:

shell
dust schedule:setup

The daemon and the application share the database, so run dust migrate before starting either. dust status prints the daemon's state alongside the database, queue, cache, and relay.

Commands

Add a cron. The command is everything after --:

shell
dust cron:add backup "0 3 * * * *" -- ./app backupdust cron:add health "* * * * * *" -- curl -fsS http://127.0.0.1:8000/health

Options: --timeout <seconds>, --overlap skip|allow|queue, --timezone <tz>, --working-dir <path>, --suspended.

Manage them:

shell
dust cron:listdust cron:list --suspendeddust cron:update backup --expression "0 4 * * * *"dust cron:update backup --timeout 300dust cron:update backup --command "./app backup --full"dust cron:suspend backupdust cron:resume backupdust cron:delete backup

Run and inspect:

shell
dust cron:run backupdust cron:runs backupdust cron:logsdust cron:prune

Control the daemon:

shell
dust cron:startdust cron:statusdust cron:stopdust cron:restart

cron:start looks for bin/dust-cron first, then for dust-cron next to the application binary. It starts the daemon with nohup and waits for its PID file. cron:stop sends SIGTERM and waits through the grace period.

Command Does
cron:add, cron:update, cron:delete Create, change, and remove a cron
cron:suspend, cron:resume Stop and restart scheduling without deleting the entry
cron:run Runs a cron once, outside the schedule
cron:list, cron:runs, cron:logs Inspect crons, their run history, and the daemon log
cron:prune Deletes old runs, optionally with --days <n>
cron:start, cron:stop, cron:restart, cron:status Control the daemon

Expressions

Six fields, with seconds first:

code
* * * * * *          every second
*/5 * * * * *        every five seconds
0 * * * * *          every minute
0 */15 * * * *       every fifteen minutes
0 3 * * * *          daily at 03:00
0 30 8 * * MON-FRI   weekdays at 08:30
0 0 1 * * *          the first day of every month at midnight

A five-field expression is accepted and starts at second 0, so 0 3 * * * and 0 0 3 * * * both mean daily at 03:00. Expressions without a timezone use CRON_TIMEZONE, which defaults to UTC; set --timezone on the cron when its schedule should follow a local clock.

Configuration

The daemon reads the framework's standard environment configuration:

Variable Default Meaning
CRON_MAX_RUNS 16 The most runs in flight at once.
CRON_LOG storage/logs/cron.log The daemon log, relative to the application.
CRON_RETENTION_DAYS 7 How many days of cron_runs to keep.
CRON_TICK_MS 250 How often the daemon checks for due crons.
CRON_GRACE_SECONDS 10 How long cron:stop waits for running crons.
CRON_TIMEZONE UTC The timezone for expressions that don't name one.

Set them in .env or in the process environment. The daemon prunes old runs every hour; cron:prune --days <n> does it on demand.

The daemon

dust-cron is one process per application. It:

  • loads the active crons and computes next_run_at;
  • checks every CRON_TICK_MS for due crons;
  • starts each run with /bin/sh -c in its own process;
  • respects overlap: skip leaves the due run for later, allow starts another, queue behaves like skip for now;
  • captures stdout and stderr to storage/logs/cron/;
  • samples CPU and peak memory from /proc;
  • updates cron_runs and the cron's last status;
  • marks runs left running by a previous daemon as interrupted on startup;
  • keeps a heartbeat row in cron_runners.

The PID file and a live-process check prevent two daemons for the same application. On stop, the daemon stops scheduling, waits CRON_GRACE_SECONDS for running crons, then kills what remains.

Overlap and failure

When a cron is due but its previous run has not finished, overlap decides what happens:

Value Behaviour
skip The due run is skipped. This is the default.
allow Another run starts alongside the first.
queue Behaves like skip for now.

Set the value with --overlap on cron:add or cron:update. Use allow only for commands that are safe to run twice at once, such as a read-only health check.

A run that exceeds --timeout is killed and recorded as timeout; its exit code is unset, and the captured output up to the kill is kept. A run that exits non-zero is recorded as failed, with the exit code and the tail of stderr, but the daemon does not alert anyone on its own. A daemon that exits without finishing its runs leaves them running; the next daemon marks them interrupted on startup so the history has no permanent ghosts. Crons do not retry automatically. If a command must retry, handle the retry inside the command or record the failure and act on it from cron_runs.

The daemon appends to storage/logs/cron.log and keeps the last 64 KiB of each run's stdout and stderr in cron_runs. Older runs are pruned after CRON_RETENTION_DAYS, which defaults to seven days, and cron:prune does it on demand.

[!WARNING] A cron command runs through /bin/sh -c, so never build one from untrusted input. Treat a cron as a shell script you deploy: restrict who can run cron:add and cron:update in production.

[!NOTE] For application work that should be visible in source control and run on every server's schedule, prefer the scheduler. Use a raw cron for shell commands the scheduler does not model, such as a one-off backup script or a process that must run outside your application.

Run history

Each row in cron_runs holds:

  • the daemon (runner_id) and the child's pid;
  • started_at and finished_at;
  • status: running, success, failed, timeout, or interrupted;
  • exit_code;
  • the last 64 KiB of stdout and stderr;
  • cpu_ms and memory_kb;
  • whether the run was scheduled or manual.

The full output stays in storage/logs/cron/.

Tables

crons holds the entries: name, expression, command, working_dir, timeout_seconds, overlap, timezone, suspended, last_run_at, next_run_at, last_status, and last_exit_code.

cron_runs holds one row per execution, indexed by (cron_id, started_at).

cron_runners holds each daemon instance, its host, PID, start time, heartbeat, and status.