❯ hailports — journal

a cheap shell script is measured in forks, not lines

a slow watchdog loop looked like a line-count problem until i counted process spawns instead, and the difference rewrote how i judge every script on the box.

2026-12-04 · engineering journal · shell · measurement · forks · discipline

what it looked like from the outside

the watchdog script on the one-machine stack had one job: sweep the live process table twice, match against two patterns, and decide whether anything had drifted from where it should be. it was maybe a few hundred lines of shell, written fast, working fine for weeks. then the machine started feeling busy whenever it ran, and the wall clock on each sweep drifted from about a second to several.

from the outside this reads like a classic bloat story. the script had grown by accretion -- another check, another edge case, another branch for a state i hadn't anticipated on the day i wrote the first version. more lines, more work, slower. that story is tidy and mostly true for most software, so i almost didn't question it.

the thing that nagged me was the shape of the slowdown. the script wasn't doing more decisions per sweep -- the process count on the box was flat, the pattern list hadn't grown, and the diff of decisions across runs was empty. same inputs, same outputs, several times the latency. bloat adds work; something else was adding cost without adding work.

so the symptom was latency growth with zero decision growth, on a script whose structure i already understood. that combination doesn't happen when the problem is line count. it happens when each line got more expensive -- and in shell, lines can get expensive in a way that has nothing to do with how much text they contain.

the wrong theory

my first theory was parsing overhead. i figured the patterns had gotten complicated enough that the matching itself was slow, and the fix would be rewriting the regexes tighter or precompiling something. this fit the evidence superficially: the loops dominated the script, the loops contained the matching, the matching was the only visible per-process work. it fit my priors too, since i've spent years being told that regex cost is where shell performance goes to die.

so i benchmarked the patterns in isolation, matched against a fixed sample, and they were fast. microseconds fast, per match, thousands of iterations without breaking a sweat. that should have killed the theory on the spot, but i did what engineers do with inconvenient benchmarks -- i assumed the real environment was somehow different from the test harness and kept looking for the slowness inside the script's own text.

the second wrong theory was io. the sweep touched the process filesystem, and i figured the virtual filesystem was throttling reads under load. this theory had the advantage of being untestable without kernel-level tooling, which made it comfortable to hold. i spent an evening on it and produced nothing but a vague feeling that the machine was 'busy' -- a feeling, i later realized, that was itself data i was refusing to read.

both wrong theories shared the same flaw: they located the cost inside the script's logic. neither of them considered that the cost might live between lines -- in the boundary the shell crosses every time it hands a fragment of work to a separate process and waits for it to come back.

what actually settled it

the measurement that decided it was a fork count, and getting a clean number took two readings rather than one. every process on a unix box gets an id from a counter that only moves forward, so the counter's drift over a window is a rough proxy for how many processes the whole machine spawned. i read the counter before the sweep, ran one sweep, read it after. the raw drift looked enormous and proved nothing by itself.

the problem with the raw number is that the counter counts everyone -- the stack's other loops, the ambient churn of a machine that never sits still. over a slow sweep's wall time, ambient drift alone can inflate the reading several times over. so i took a second reading over the same wall clock with the script idle, subtracted that ambient advance from the sweep reading, and what remained was the script's own share. the method is crude but honest: advance during the run, minus advance during an equally long idle window, is the script's footprint.

that number settled it immediately. the sweep was spawning a fork for every process it examined, twice over, because each pattern evaluation was wrapped in a command substitution -- the shell building a child process to run a tiny snippet, receiving one line back, throwing the child away, and repeating that a few hundred times per pass. the matching was free. the childbirth was the entire bill.

in the day job this is folklore, the kind of thing you learn by being near someone who already knows it. shell cheapness is process count, not line count. a function body is free; summoning it through a substitution is not. i had the rule filed away under 'matters for big systems' and had quietly decided my little machine was too small for it to apply. the fork count disagreed.

the fix

the fix was small enough to be embarrassing. everywhere i'd wrapped a match in a substitution to capture a yes-or-no answer, i replaced it with a construct that answers inside the current shell -- no child, no capture, no fork. same patterns, same inputs, same decisions. the sweep's latency collapsed back to about a second, and the machine stopped flinching every time the watchdog woke up.

but i didn't trust the swap on intuition, because 'same decisions' is exactly the kind of claim shell code loves to betray. a substitution and its replacement can differ in quoting, exit-status handling, whitespace, edge cases around empty output -- any of which can flip a decision silently. so i ran both versions over the entire live process table, every process, both patterns, and diffed the decision sets. a few hundred processes, twice each, zero mismatches.

that diff is the part i'd skip on a weekend project and never skip on a production system, and the whole point of this stack is that i stopped letting 'weekend project' be an excuse. the swap took an hour. the proof took twenty minutes and cost nothing but discipline. without it, the fix is a vibes-level improvement; with it, it's a behavior-preserving replacement i can defend if the watchdog ever makes a call i have to explain.

what i deliberately did not change: the script's structure, its output format, its sweep cadence, or the two patterns themselves. the temptation after any performance win is to refactor everything the light touches, and the discipline is to touch exactly one variable and leave a clean attribution behind. if latency regresses later, i want to know it was the forks and not something i smuggled in under the same commit.

the rule i keep now

the rule i keep now is that before optimizing any shell loop, i count forks before i count lines, and i count the script's own forks -- ambient drift subtracted over matched wall time -- because raw counter advance measures the whole machine and will overstate anything that runs slowly enough to give the background room to churn. and i no longer assume a rewrite is safe: the old path and the new path both run over every live input, and the decisions get diffed, because a performance fix that changes behavior isn't a fix, it's a trade i never agreed to. none of this is clever. it's the same loop i run at work -- measure the real cost, isolate the variable, prove equivalence -- pointed at a machine one person owns. the code was never the hard part; deciding that a one-person side business deserves the same evidentiary standard as anything with other people's money behind it was, and once i decided that, the rest was an afternoon.

the rule: count the forks a script spawns itself, not its lines, and prove any swap behavior-preserving by diffing decisions across every live input.

related field guides

earlier entries

see the machine this came out of

the board is public. the scan is free. the playbook is the long version of entries like this one.

← journal · hailports