I spent a day hardening a deployment script to fail loudly on partial delivery. Then I ran it in a way that made every one of those failures invisible.
The tool moves multi-gigabyte container images onto an edge device. It had a real
bug: a run could exit 0 having delivered half the list. I changed the script to check
the device afterwards instead of trusting the loop’s own bookkeeping, to treat “could
not verify” as a failure, and to catch SIGINT/SIGTERM and report what is still
outstanding.
| tail prints nothing until the script exits
python3 script.py 2>&1 | tail -25
tail -25 prints nothing until its input ends, because it cannot know which lines are
last. On top of that, a pipe switches the script’s stdout from line-buffered to
block-buffered. For a thirty-minute job that means thirty minutes without output,
and if you kill it, no output at all.
I watched the script across three attempts, saw no output and concluded it had
stalled, though it was working. I ended up inferring progress from pgrep output,
because I’d thrown away the log the tool was already writing.
python3 -u script.py 2>&1
-u for Python, stdbuf -oL for most other things. If you need to filter, write the
output to a file and filter the file afterwards.
find … | head -1 picks an arbitrary match
D=$(find /tmp -name "work-*" -type d | head -1)
du -sh "$D" # 0 bytes, so "it stalled"
There were three work-* directories: two leaked from interrupted runs, one live.
head -1 returned a dead one, and twice I reported a stall that was not happening.
head -1 is safe only when you know there is one match. When there might be several,
it returns whichever find lists first. Sort by mtime, or read the path from the
running process’s command line:
pgrep -fl mytool
The command line has the real path in it.
Killing a process destroys what it owns
The tool downloads to a temp dir and cleans up in a
finally:. I killed it mid-run, then tried to reuse the 9.4 GB file it had just
finished downloading:
cat: /var/folders/.../work-p5rdi7e0/image.tar: No such file or directory
The cleanup did what it should. My mistake was treating another process’s scratch
space as a durable artifact. The thirty-minute download was gone, and I found out
from a cat error.
If you want an intermediate result to survive, move it somewhere the tool does not own before you interrupt anything.
I measured the wrong thing each time
In all three cases I trusted a measurement without checking whether I had measured
the right thing. The pipe showed the state of a buffer. The find showed a dead
directory. The cat showed a path with no file behind it any more.
Before acting on a reading, check where it came from: an unbuffered stream, the path
from pgrep -fl, a file outside the tool’s temp dir.