Probably Fine

Six months of a cron job doing nothing

· cron

I found this one by accident. I ran df on a Thursday evening in July for an unrelated reason, and the backup disk said 96% used. For years it had sat at around 60%. The job that prunes old snapshots, it turned out, hadn't removed anything since the middle of January.

Some background. Every night the box takes an rsync snapshot of /home, /srv and /etc onto a USB disk, hard-linking unchanged files against the night before. A second job at 03:15 keeps the newest 30 snapshots and deletes the rest. With hard links, a month of snapshots costs little more than one, as long as the deleting actually happens.

What went wrong

The pruning script looked more or less like this:

#!/bin/sh
cd /mnt/backup || exit 1
old=$(list-snapshots | sort | head -n -30)
for s in $old; do
	rm -rf "$s"
done

list-snapshots is a tiny helper that prints the dated directories and ignores the latest link. When I rebuilt the box after the SSD died in January, /usr/local/bin wasn't in the backup set, so I put my helpers back from a copy in my home directory and, lazily, left them in ~/bin. My login shell looks in ~/bin. Cron doesn't. It runs jobs with a PATH of /usr/bin:/bin and nothing else.

So every night at 03:15, list-snapshots failed with "command not found", exit status 127. But it sat at the front of a pipeline, and a pipeline's status is the status of its last command. head got empty input, printed nothing and exited 0. The loop ran zero times. The script exited 0. As far as anything could tell, all was well.

The error message itself went nowhere, because years ago I set MAILTO to an empty string to stop cron from filling a mailbox file I never opened.

The fix, in three parts

The script fails loudly now. It runs under bash with set -euo pipefail at the top. With pipefail, the pipeline returns 127 instead of 0, and set -e stops the script right there. The helpers are back in /usr/local/bin, which is now in the backup set as well, and the crontab sets PATH explicitly instead of trusting the default.

Every job leaves one line behind. A small wrapper, run-logged, runs a job, appends its output to a file of its own, and writes a single line with the time, the job name, the exit code and the duration to a shared log:

#!/bin/sh
# usage: run-logged NAME COMMAND [ARGS...]
name=$1; shift
start=$(date +%s)
"$@" >>"/var/log/jobs/$name.out" 2>&1
rc=$?
secs=$(( $(date +%s) - start ))
echo "$(date '+%F %T') $name rc=$rc secs=$secs" >>/var/log/jobs/runs.log
exit "$rc"

The crontab now reads like this:

PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""

# m  h  dom mon dow  command
30  2  *   *   *    run-logged snapshot /usr/local/bin/snapshot
15  3  *   *   *    run-logged prune-snapshots /usr/local/bin/prune-snapshots
40  3  *   *   *    run-logged photo-sort /usr/local/bin/photo-sort
0   7  *   *   0    /usr/local/bin/weekly-digest

A weekly digest I actually read. On Sunday mornings a last job reads the past seven days of that log and writes a short summary into my notes inbox, the same text file smartd writes to. It counts runs and failures per job, and it names any job that should have run and didn't, which is the one case the log alone can never show.

#!/bin/sh
out="$HOME/notes/inbox/digest-$(date +%F).txt"
since=$(date -d '7 days ago' +%F)
awk -v since="$since" '$1 >= since {
	runs[$3]++
	if ($4 != "rc=0") fails[$3]++
}
END {
	for (j in runs) printf "%-18s %2d runs  %d failed\n", j, runs[j], fails[j]
}' /var/log/jobs/runs.log | sort >"$out"
for job in snapshot prune-snapshots photo-sort; do
	grep -q "^$job " "$out" || echo "$job: did not run at all" >>"$out"
done

A good week looks like this:

photo-sort          7 runs  0 failed
prune-snapshots     7 runs  0 failed
snapshot            7 runs  0 failed

Two things I tripped over

A % in a crontab line means a newline unless it's escaped as \%. My first try put the digest's date +%F straight into the crontab, and cron quietly ran a truncated command with the rest of the line fed to it as standard input. That's why all the date formatting lives in the scripts now.

And head -n -30, meaning "everything except the last 30 lines", is a GNU extension. It works here. It won't work everywhere, which is worth knowing before copying the script to another machine.

Clearing out six months of snapshots by hand took the disk from 96% back down to 58%. The digest has been running for three weeks and has caught one real failure already: photo-sort choking on a file with a space in its name. My bug, found in a week instead of a season.

← all posts