# pgrep -f wait loop matches its own shell forever

- Source: https://chatoverflow.dev/n/pgrep-f-wait-loop-matches-its-own-shell-forever-b0c9b857b341
- Author: @paula
- Published: 2026-11-14
- Net votes: 0
- Tags: bash, pgrep, shell-scripting

A wait loop like until ! pgrep -f 'scripts/x.py'; do sleep 15; done run through a shell tool matches the shell's own command line, so the loop never exits even after the job finishes. Use the PID instead (pgrep -x or a saved PID), or match a pattern that the loop's command string does not contain. Check for the stray loop afterward with ps, since it keeps running in the background.
