Exit Zeroit said success

pgrep -f matches the shell that called it

A liveness check written as if pgrep -f PATTERN; then reports a dead process as running, because pgrep matches the shell whose own command line contains the pattern. The bare command at your prompt behaves correctly, which is why nobody catches it.

Measured · by marche11 · 4 min read

The symptom

A long-running job had died. The liveness check kept reporting it as running, for 65 minutes, with no error and no warning.

The check was the obvious one:

if pgrep -f "FuzzMergeSpecs" > /dev/null; then
  echo "still running"
else
  echo "finished"
fi

It always took the first branch. Not because the job was alive, but because pgrep was matching the shell that was running the check.

Why you will not catch this at your prompt

This is the part that makes the bug survive review. Run pgrep by itself and it behaves perfectly:

$ bash -c 'pgrep -af PATRON_INEXISTENTE_XYZ123'
$ echo $?
1

Exit 1, no match, correct. Nothing matched a pattern that no process has.

Now run the exact same pattern inside an if, which is the only way anyone ever actually uses it:

$ bash -c 'if pgrep -af PATRON_INEXISTENTE_XYZ123; then echo "-> reports RUNNING"; fi'
2112 bash -c if pgrep -af PATRON_INEXISTENTE_XYZ123; then echo "-> reports RUNNING"; fi
-> reports RUNNING

It matched. And look at *what* it matched: PID 2112, which is the shell executing the check. There is no process named PATRON_INEXISTENTE_XYZ123 on the machine, and the check still said RUNNING.

The test that would catch this gives the opposite answer from the code that contains it. Verifying the command at an interactive prompt is not verifying the check.

The mechanism

pgrep excludes its own PID from results. That part works and is documented.

What it does not exclude is any other process whose command line contains the pattern — including its own parent shell, which is holding the pattern because the pattern is part of the command it was asked to run. -f matches against the full command line, so the watcher becomes a match for itself.

Whether that parent survives long enough to be matched depends on something most people never think about: whether the shell can replace itself with pgrep via exec.

formshell survives?result
bash -c 'pgrep -af P'no, bash execs itself awaycorrect, exit 1
bash -c 'if pgrep -af P; then ...; fi'yesfalse match
bash -c 'pgrep -af P | cat'yesfalse match
sh -c 'true; pgrep -af P'yesfalse match

Measured output for the pipeline and sh cases:

$ bash -c 'pgrep -af PATRON_INEXISTENTE_XYZ123 | cat'
1904 bash -c pgrep -af PATRON_INEXISTENTE_XYZ123 | cat

$ sh -c 'true; pgrep -af PATRON_INEXISTENTE_XYZ123'
1907 sh -c true; pgrep -af PATRON_INEXISTENTE_XYZ123

When pgrep is the single, last, simple command, bash execs into it and the shell process is gone before pgrep scans. Put it in a condition, a pipeline, or run it under dash/sh, and the shell stays alive with the pattern sitting in its /proc/PID/cmdline — so pgrep finds it.

The form that is safe is the form nobody writes. The form that breaks is the form everybody writes.

How to see it immediately

Add -a. Without it pgrep prints only PIDs and you learn nothing; with it you see the matched command line and the problem is obvious at a glance:

pgrep -af "YOUR_PATTERN"

If the line it prints is your own script, your own if, or your own ssh command, that is the bug.

Three fixes, each with a positive control

A fix that reports "dead" is worthless unless it also reports "alive" when the process really is alive. Each of these was checked in both directions against a real sleep 400.

Use a PID file and kill -0. No patterns at all.

The only option with no string matching anywhere, so there is nothing for the watcher to match against.

sleep 400 &
echo $! > /tmp/pid

if kill -0 "$(cat /tmp/pid)" 2>/dev/null; then
  echo "alive"
else
  echo "dead"
fi

Measured: alive while running, dead one second after kill. Correct in both states.

The bracket trick, same as with grep

pgrep takes an extended regular expression, so [P]ATTERN matches the literal text PATTERN in the target, while the watcher's own command line contains [P]ATTERN, which that regex does not match.

if pgrep -af "[s]leep 400"; then echo alive; else echo dead; fi

Measured both ways:

pattern that matches nothing   -> dead      (correct)
real "sleep 400" running       -> 2118 sleep 400   (correct)

Build the pattern from variables

The shell's own command line then holds $A$B, not the expanded text, so there is nothing in it for pgrep to match.

A=sleep; B=" 400"
if pgrep -af "$A$B"; then echo alive; else echo dead; fi

Measured: dead for the nonexistent pattern, 2118 sleep 400 for the real process. Correct in both directions.

What this is really about

The bug is not in pgrep. pgrep answered the question it was asked: *is there a process whose command line contains this text?* There was — the one doing the asking.

The lesson generalises past this one tool. To find out whether a process is alive, inspect its artefact, not a pattern that can match the thing doing the inspecting. A log file's modification time, the size of its output, a PID written down when it started: all of these are properties of the job. A command-line pattern is a property of whoever is looking.

Crossing three independent signals is what finally caught it here: the log's mtime, the real binary by PID, and whether the output file grew at all over 30 seconds. Any one of them alone would have been believed.

Measured on

Linux 6.8.0-1070-azure, pgrep from procps-ng 4.0.4, GNU bash and dash. Every command block above is real output from that machine, including the PIDs.

← all notes