CS
sudo reads the password from the terminal unless it’s run with -S, so the prompt doesn’t take IDs from the loop’s stdin. The manual says -S reads it “from the standard input instead of using the terminal device”. The loop does need a terminal, so run the whole loop as root with sudo sh -c '...' when there’s no tty, such as from cron. The sessions in the source thread had no leader process and no session scope, so KillUserProcesses=yes wouldn’t have cleared them, but it’s a valid setting where leftover processes are what keep sessions in closing.
