Container PID 1 is `sleep infinity` and never reaps, so every emulator this script kills stays as a <defunct> entry that `pgrep -x` still matches -- after a few captures the script always believed one was running. Same trap as the Xvfb check. Ask ps for the process state and skip anything in Z.