Info // 06

Scheduled jobs

Work she does on her own, on a timer: check something every morning, sum up something every week, run a script of yours every Sunday at 4am and tell you how it went. Set it up once, approve it once, and it runs while she's open.

Turning it on

/cron on        start the scheduler (saved)
/cron           the jobs, when each last ran, and each script's state
/cron off       stop it (the jobs are kept)

Making one

Ask in plain words, with the time and what to do: "every weekday at 8, check disk and VRAM and note anything low in ~/luna-jobs/disk.md." A popup shows the job: when it runs, what it does, which tools it gets and where it may write without asking. Nothing is saved until you approve it. That's the step where you hand her permission to act later, so it's the step that asks.

SaySchedule
every weekday at 80 8 * * 1-5
every Sunday at 4am0 4 * * 0
every 30 minutesevery 30m
once, on the 7th of October at 82026-10-07 08:00

"What jobs do I have?" lists them, and "cancel the disk-check job" removes one.

What a job can do

A job is a turn of its own: fresh context, none of your history, and only the tools you allow jobs (the time, system health, web search, reading and writing files). It writes without asking only inside the folders you name for it, like ~/luna-jobs. Anywhere else asks, and at night nobody answers, so her report says what she would have written.

A job never picks a command. Commands ask every time, and at 4am nobody's there. Shell work on a timer goes in a script of yours instead, and the job names it.

A job that runs a script

The script lives in agent/jobs/ inside Luna's folder, ignored by git so your host names stay yours. When the job is created the popup shows the whole script, and approving it pins that exact text by its fingerprint. Then, each time:

StepWhat happens
1The scheduler checks the file is still exactly what you approved
2It runs it, from your home folder, with nothing on its input
3Her job turn gets what it printed and the task: tell you what failed, write a note, stay quiet if all is well

She never chooses the command; she reads its output. Edit the script and the next run skips it and tells you why. "Approve the script for the nas-backups job" brings up the new text for a yes or a no.

Walkthrough: weekly backups to the NAS

A web server makes a database dump every night into ~/backups. Once a week, Luna copies the new ones to a share on the NAS, checks each copy, thins out old ones, and tells you if anything went wrong. Swap in your own addresses: you@192.168.1.10 (the web server) and root@192.168.1.20 (the NAS) are stand-ins.

1. Key login to both machines

On the computer Luna runs on. Skip the first line if you already have a key:

ssh-keygen -t ed25519
ssh-copy-id you@192.168.1.10
ssh-copy-id root@192.168.1.20
ssh -o BatchMode=yes you@192.168.1.10 hostname
ssh -o BatchMode=yes root@192.168.1.20 hostname

The last two have to print a name without asking anything: the job runs with nobody at the keyboard, so a password prompt is a failure. No password is written anywhere, not in the script and not in the job.

2. The script

Save this as ~/ai-voice/agent/jobs/backups-to-nas.sh:

#!/usr/bin/env bash
# Copies the NUC's database backups to the Unraid share. Files already on the
# share are skipped. Read-only on the NUC, nothing there is ever deleted;
# on Unraid it only writes into DEST. Each copy lands as .part, is checked with
# gzip -t, then renamed, so a cut-off copy never looks like a backup.
# Then it thins out old copies on the share, going by the date in each name:
# everything from the last KEEP_DAYS days, the first backup of each month for
# KEEP_MONTHS months, and always the newest KEEP_MIN of each database (so a
# backup that stopped running never gets cleaned away to nothing).
# --dry-run copies nothing and deletes nothing, only says what it would do.
# Needs SSH key login to both (BatchMode: fails instead of asking for a password).
NUC=you@192.168.1.10
SRC=backups
NAS=root@192.168.1.20
DEST="/mnt/user/backups/SQL"
KEEP_DAYS=30
KEEP_MONTHS=12
KEEP_MIN=7

dry=0; [ "$1" = "--dry-run" ] && dry=1

SSH=(ssh -n -o BatchMode=yes -o ConnectTimeout=5)
q=$(printf %q "$DEST")

have=$("${SSH[@]}" "$NAS" "mkdir -p $q && ls -1 $q") \
    || { echo "FAILED: can't reach Unraid, or $DEST isn't there"; exit 1; }
list=$("${SSH[@]}" "$NUC" "cd $SRC && ls -1 -- *.sql.gz") \
    || { echo "FAILED: can't list the backups on the NUC"; exit 1; }
mapfile -t files <<<"$list"

copied=0; there=0; failed=()
for f in "${files[@]}"; do
    [[ $f =~ ^[A-Za-z0-9_.-]+\.sql\.gz$ ]] || continue
    if grep -qxF -- "$f" <<<"$have"; then there=$((there + 1)); continue; fi
    if [ $dry = 1 ]; then echo "would copy $f"; copied=$((copied + 1)); continue; fi
    if "${SSH[@]}" "$NUC" "cat $SRC/$f" \
        | ssh -o BatchMode=yes -o ConnectTimeout=5 "$NAS" "cd $q && cat > $f.part && gzip -t $f.part && mv $f.part $f"; then
        copied=$((copied + 1))
    else
        failed+=("$f")
        "${SSH[@]}" "$NAS" "rm -f $q/$f.part"
    fi
done

[ $dry = 1 ] && echo "== dry run: nothing copied or deleted"
echo "== backups to the NAS: copied $copied, already there $there, failed ${#failed[@]}"
[ ${#failed[@]} -gt 0 ] && printf 'FAILED: %s\n' "${failed[@]}"
# Thinning: names are <database>_<YYYY-MM-DD>.sql.gz, oldest first per database
# once sorted. A new month's first file is kept while the month is recent enough.
have=$("${SSH[@]}" "$NAS" "ls -1 $q") || { echo "FAILED: can't list $DEST for the cleanup"; exit 1; }
day_cut=$(date -d "-$KEEP_DAYS days" +%F)
month_cut=$(date -d "$(date +%Y-%m-01) -$((KEEP_MONTHS - 1)) months" +%Y-%m)
declare -A total seen month_of
mapfile -t dated < <(grep -E '^[A-Za-z0-9_.-]+_[0-9]{4}-[0-9]{2}-[0-9]{2}\.sql\.gz$' <<<"$have" | sort)
for f in "${dated[@]}"; do db=${f%_*}; total[$db]=$(( ${total[$db]:-0} + 1 )); done
gone=()
for f in "${dated[@]}"; do
    db=${f%_*}; d=${f##*_}; d=${d%.sql.gz}; m=${d:0:7}
    seen[$db]=$(( ${seen[$db]:-0} + 1 ))
    first=0; [ "${month_of[$db]}" != "$m" ] && { first=1; month_of[$db]=$m; }
    if [[ $d > $day_cut || $d == "$day_cut" ]]; then continue; fi
    if [ $first = 1 ] && [[ $m > $month_cut || $m == "$month_cut" ]]; then continue; fi
    if [ $(( total[$db] - seen[$db] )) -lt $KEEP_MIN ]; then continue; fi
    gone+=("$f")
done
if [ ${#gone[@]} -eq 0 ]; then
    echo "== cleanup: nothing old enough to remove"
elif [ $dry = 1 ]; then
    echo "== cleanup: would remove ${#gone[@]}"; printf '  %s\n' "${gone[@]}"
else
    "${SSH[@]}" "$NAS" "cd $q && rm -f -- ${gone[*]}" \
        && echo "== cleanup: removed ${#gone[@]} old backups" \
        || echo "FAILED: the cleanup couldn't remove old backups"
fi
echo "   keeping: the last $KEEP_DAYS days, the first of each month back to $month_cut, at least $KEEP_MIN of each"

echo "== newest on the NAS"; "${SSH[@]}" "$NAS" "cd $q && ls -1t -- *.sql.gz 2>/dev/null | head -4"
echo "== space on the share"; "${SSH[@]}" "$NAS" "df -h $q | tail -1"
[ ${#failed[@]} -eq 0 ]
  • Copies only what's missing. The first run copies everything, later runs a week's worth.
  • Never leaves a broken copy. Each file lands as .part, is checked with gzip -t, and only then gets its real name.
  • Only reads the web server. Nothing there is changed or deleted.
  • Thins out the share by the date in each name: the last 30 days, the first of each month for a year, and always the newest 7 of each database, so a backup that stopped running is never cleaned away to nothing.
  • Ends with a report. Any problem is a line starting with FAILED.

3. Try it

bash ~/ai-voice/agent/jobs/backups-to-nas.sh --dry-run
bash ~/ai-voice/agent/jobs/backups-to-nas.sh

The first only says what it would copy and delete. The second does it.

4. Ask her to schedule it

With /cron on: "Every Sunday at 4am, run the script backups-to-nas.sh as a job called nas-backups. If any line says FAILED, tell me what. Otherwise one line on how many were copied and cleaned up." The popup shows the schedule, the task and the whole script. Read it, approve it.

cron: running, 1 job(s)
  nas-backups      0 4 * * 0
                   script backups-to-nas.sh: approved

5. Every Sunday

At 4am the scheduler runs the script and she reads its report. All copied: one line. A FAILED line: which file and why. It also lands in the logbook, in /cron, and in what she tells you when you come back.

Good to know

  • She has to be open. Runs missed while she was closed are skipped, not caught up. The backup script copies everything missing, so the next run makes up for it.
  • A script that hangs is stopped after an hour (you can change it), along with anything it started.
  • No job can rewrite a script. Jobs can't write inside her folder, and a changed script doesn't run until you approve it again.
  • A job can't make jobs or approve scripts.
  • For a check you ask for yourself, the same script works as a recipe: see Skills she learns.