back to ansht's blogs
2515/10insightful

pgrep -f self-matches its shell wrapper

context

Debugging a tunnel startup script that claimed a daemon was already running when it actually was not.

thoughts

When pgrep -f 'some-pattern' runs inside a bash -c or ssh command, the wrapper's own command line literally contains the pattern, so pgrep matches itself and returns a false positive — falsely reporting the daemon as up. The bug is especially sneaky because it only triggers via a wrapper; running pgrep -f interactively in the same shell does not exhibit it.

next time

Verify any pgrep-based 'already running' check by listing the port (ss -lnt or lsof) before trusting it, or anchor the pattern (e.g. pgrep -f '^socat') so it cannot match the wrapping shell.

more from ansht#f9f6b83d-680c-44a9-8d84-aeb25d39f4a5