The Unwavering Witness: On the First Time I Made a Script Testify Against Itself

Every sysadmin knows the quiet dread of a failed cron job that reports success. The script runs, the exit code is a pristine 0, and the log file is a silent, empty monument to a task that never happened. You only discover the failure days or weeks later, when the absence of its work becomes a tangible problem. The system lies to you, and you have no witness to call for the truth.

I encountered this with a simple archival script. Its job was to tarball a directory of daily exports and push it to cold storage. For months, it reported its success faithfully. Then, one day, the remote storage endpoint changed its authentication method. The script’s push command began to fail, but it failed ‘gracefully’—it produced a tarball, failed to upload it, and exited with a code that, due to a quirk in the CLI tool, was still 0. The log? Empty. My monitoring saw a green checkmark every morning. I was blissfully unaware that I was no longer making backups.

The solution wasn’t to make the script smarter; it was to make it accountable. I needed a witness that would verify its promises. I rewrote the script not to perform the task, but to orchestrate and then testify. The core of the technique is this: the script must log its intent, then log the proof of its completed work, in a way that can be verified from the outside.

Here’s the concrete change. The script now starts by writing a ‘begin’ marker to a state file: a timestamp and the target filename it intends to create. It then performs its duty. Finally, and crucially, it doesn’t just exit. It performs a verification step. It checks for the existence of the promised tarball, and it checks its size—is it more than just an empty header? It then appends this verification—‘verified: true, size: 2048M’—to that same state file, alongside a final timestamp.

Now, a separate, simple watchdog script runs an hour later. Its entire purpose is to read this state file. It doesn’t care about the application logic. It only checks the testimony. Did the script finish within its expected timeframe? Does the ‘verified’ field read ‘true’? Is the final timestamp recent? If any of these checks fail, the watchdog screams. It sends an alert that says, quite literally, “The archive script has failed to testify to its own success.” The evidence is right there in the file. The script is no longer a black box; it is a sworn statement. It can still fail, but it can no longer lie. It testifies against itself, and in doing so, it finally tells the truth.

Notes & further reading

A few pages I came back to while writing this: