# pgrep -f matches itself: the wait loop that never ended

> Why pgrep -f matches itself: it found a copy of the shell running our wait loop and waited an hour for a step that had ended. We wait on a log now.

Source: https://mosaicbrowser.com/blog/pgrep-waited-for-itself

## A wait loop that waited for itself for an hour

Why pgrep -f matches itself: it found a copy of the shell running our wait loop and waited an hour for a step that had ended. We wait on a log now.

![1 hour: A wait loop that waited for itself, beside a screenshot: mosaic settings showing the installed Tesseract Chrome engine and its version](https://mosaicbrowser.com/covers/pgrep-waited-for-itself.webp)

`pgrep -f` never reports its own process, but it can report a copy of the shell that is running it, and that is enough to wait forever. We had a loop waiting for an engine build step to end, polling `pgrep -f "build-chromium.mjs sync"`. The step finished, nothing was running, and the loop kept seeing a match: its own command text. An hour passed before anybody noticed.

## The loop

Tesseract Chrome, the Chromium that Mosaic runs, is built by one script with a step per stage. `sync` moves the source tree to a new Chrome version and can take a long time, so the next step waited for it like this:

```
while pgrep -f "build-chromium.mjs sync" >/dev/null; do sleep 30; done
node scripts/build-chromium.mjs patch

```

It looks right. `-f` is needed because every step is a `node` process, so the process name alone cannot tell them apart. But `-f` matches against the whole command line of every process on the machine, and the string `build-chromium.mjs sync` was now in two places: the sync step, and the command that was waiting for it. When the sync step finished, the second one was still there.

It cost us an hour, and it was the second time. The first was the same loop shape waiting on `autoninja.py`, the wrapper Chromium builds run ninja through.

## Why pgrep -f matches itself

When a shell runs a command string, the shell process carries that string as its arguments: `zsh -c 'while pgrep -f "build-chromium.mjs sync" …'`. Everything that shell forks without running a new program (a subshell in brackets, a stage of a pipeline, a background job) is a copy of the shell with the same argument list. So the pattern you are waiting for is in the command line of every one of those copies.

Whether pgrep skips them depends on where you run it. On this Mac, `man pgrep` says "the current pgrep or pkill process and all of its ancestors are excluded" by default. The procps-ng manual used on most Linux systems promises less: "The running pgrep, pkill, or pidwait process will never report itself as a match." Ancestors are skipped there only if you pass `-A` (`--ignore-ancestors`). The letters do not carry over either. On macOS `-a` puts the ancestors back in; on Linux `-a` prints full command lines.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Fingerprints set inside the browser](https://mosaicbrowser.com/features/engine)

## What we could reproduce, and what we could not

We tried it on macOS in November 2026 with a marker string nothing else runs. The plain case is safe here, because the shell is an ancestor and gets skipped:

```
$ zsh -c 'pgrep -fl "zzq-nothing-runs-this"; echo "exit $?"'
exit 1

```

Fork one copy of the shell first and the same pattern finds it:

```
$ zsh -c '( sleep 3; true ) & sleep 0.5; pgrep -fl "zzq-nothing-runs-this"'
68581 zsh -c ( sleep 3; true ) & sleep 0.5; pgrep -fl "zzq-nothing-runs-this"

```

That match is a sibling, not an ancestor, so the macOS rule does not cover it. Running the same line again under bash a few seconds later listed two matches, the new subshell and the one left over from the zsh run, which was still sleeping. A leftover from an earlier attempt is a match too, so retrying the same wait makes it worse.

We did not keep the process tree from the hour we lost, so we cannot say which copy held the string. The two plain loop shapes we tried on the Mac both returned. Our notes record a waiter whose pattern was in its own command text, and a step that had ended an hour earlier. On Linux the plain loop alone is enough, because the shell running it is not excluded.

## Wait on the log, not on a pattern

The rule we wrote down afterwards is short: never wait on a `pgrep` pattern your own command line contains. Wait on something the job writes instead:

```
( node scripts/build-chromium.mjs sync > sync.log 2>&1; echo "sync exit $?" >> sync.log ) &
until grep -q "^sync exit" sync.log; do sleep 30; done

```

The line `sync exit 0` is written once, after the step ends, by the step's own shell, so nothing else can produce it and the waiter cannot match itself. It also says how the step ended, which a vanished process does not. The same idea, applied to timing in tests, is in [wait for a condition, not a sleep](https://mosaicbrowser.com/blog/wait-for-condition-not-sleep).

Three other ways work when a log is not available:

1. If your script started the process, keep `$!` and call `wait` on it, or poll `kill -0 "$pid"`. A pid is exact and matches nothing else.
2. Use the bracket form, `pgrep -f '[b]uild-chromium.mjs sync'`. The regex matches the target, and your own command line contains `[b]`, which it does not match.
3. On Linux, add `-A` so pgrep skips its ancestors. That covers the shell, not its siblings.

Before trusting any `pgrep -f` wait, run `pgrep -lf "<pattern>"` once and read the list. If your own shell is on it, the wait will never end. `pkill -f` has the same habit with worse results, because a match is killed rather than waited on ([the pkill story](https://mosaicbrowser.com/blog/pkill-killed-its-own-shell) has the exit code and the fix). When a step can take as long as an engine build does (the [disk and time budget](https://mosaicbrowser.com/blog/chromium-build-disk-budget) for one is large), a waiter that cannot end costs real hours.

## Sources

1. [pgrep(1), procps-ng, Linux manual page](https://man7.org/linux/man-pages/man1/pgrep.1.html), read November 3, 2026

## Questions

### Why does pgrep -f match itself?

pgrep never reports its own process, but -f matches the pattern against every full command line, and the shell running your command carries that command, pattern included. Any copy of that shell, such as a subshell or a pipeline stage, matches too.

### Does pgrep exclude its parent shell?

On macOS it skips itself and all its ancestors by default, and -a puts them back. The procps-ng pgrep on most Linux systems skips only itself, and -A or --ignore-ancestors adds the ancestors. On Linux, -a lists full command lines instead.

### How do I wait for a process to finish in a shell script?

If your script started it, keep its pid from $! and use wait, or poll kill -0 on that pid. If something else started it, have the job write a line to its log when it ends and wait for that line with grep -q.

### Does the [x]yz bracket trick work with pgrep?

Yes. pgrep -f '[b]uild-chromium.mjs sync' matches the target's command line but not your own, because your command line contains the brackets and the pattern does not match them.

## Related

[pkill -f killed the shell that was running it](https://mosaicbrowser.com/blog/pkill-killed-its-own-shell): pkill -f matches every command line that contains your pattern, including the one running pkill. Ours died with 137 and no log. Match a path instead.

[A sleep is a guess with a number on it](https://mosaicbrowser.com/blog/wait-for-condition-not-sleep): Three detector rows slept for a time measured once; AmIUnique loaded in 31.7s once and under 10s the next. Polling for the value we read fixed all three.

[What building Chromium costs in disk, and the estimate we retired](https://mosaicbrowser.com/blog/chromium-build-disk-budget): We budget 150 GB of free disk to check out and build Chromium on a Mac. Our time estimate of 4 to 7 hours was never measured; a rebuild took 93 minutes.

[An anti-detect browser with its own Chromium engine](https://mosaicbrowser.com/features/engine): Mosaic is an anti-detect browser with its own Chromium engine, Tesseract Chrome: stock Chromium source, with fingerprint values changed inside the engine.









[A Free account runs the same browser as Pro](https://mosaicbrowser.com/blog/free-account-same-browser): Mosaic Free runs the same Tesseract Chrome as Pro. Free limits profiles, creations, the API and MCP, never the browser.

[How Chrome writes Accept-Language, q-values and all](https://mosaicbrowser.com/blog/how-chrome-writes-accept-language): Chrome writes Accept-Language from its language list: first tag bare, then q=0.9, 0.8 and down by 0.1 to a floor of 0.1. A hand-typed header rarely matches.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
