kill -0 on a root-owned process fails for a normal user, so a wait loop stops too early
Symptom#
A stop script starts tools with setsid sudo -n perf ... &, saves $!, later sends them SIGINT, and waits with kill -0 $pid until they exit. Run as a normal user, that loop ends at once, so the next step can read empty or half-written output files.
Cause#
The saved pid is a root process (sudo, then the tool). A normal user may not signal it, so kill -0 returns non-zero with "Operation not permitted". The loop reads that as "the process is gone".
Fix#
Check liveness with the same rights you used to start it:
for; do || ; done
for; do
alive=0
for; do && alive=1; done
[ && break
done
Under set -e, end each signal command with || true: a pid that already exited fails the line.
Evidence#
Caught in review of the dpucrumbs vendor-tool observers (perf stat, record and sched started as root around each repetition) before the first run, from the Unix rule that a user may not signal another user's process. With sudo -n kill -0 the loop then waited until perf printed "Captured and wrote ... perf.data", and the files were complete.
Also found by searching
- kill: (1234) - Operation not permitted
- wait loop thinks a sudo process already exited
- how do I check if a root process is still alive from a normal user
- stop script moves on before perf finished writing its file