pgrep -f Matches Itself: Why Your "Wait for the Job" Loop Never Ends (and the PID-File Fix)
A common way to wait for a background job in a shell script is to poll for it by name: while pgrep -f "train.py" > /dev/null; do sleep 10; done echo "training finished" It looks right, and in an interactive terminal it usually works. But I've had this loop never finish twice on my home server, long after the job had exited. Both times the cause was the same: pgrep -f was finding a process that wasn't the job. Reproducing it This script starts a fake job (a 3-second sleep renamed to look like python train.py ), then waits for it with the loop above, run through bash -c , the way cron, ssh, nohup and many tools start commands: #!/bin/bash # Reproduce: a waiter started with `bash -c "..."` matches its own command line. # A fake "training job" that runs for 3 seconds: bash -c 'exec -a "python train.py" sleep 3' & # The waiter, started the way cron / ssh / nohup / systemd often start things: timeout 10 bash -c...